title: Reading index file from URL with HTTP Socket Programming in Java
body: <p>my problem is, i can't read the text from a given URL using HTTP sockets. For example i want to read from 88.88.8.8/text.txt and print the contents on command line yet it get stuck at the reading part without any errors:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.util.Base64;
  
  public class Program {
  
      static final int PORT_NO = 80;
  
      static Socket m_socket;
  
      static String m_server;
      static String m_index;
      static String m_authString;
  
      public static void main(String[] args) throws IOException {
  
          try {        
              setConnectionInfoFromArgs(args);         
              m_socket = new Socket(m_server, PORT_NO);
  
              loginToServer();
  
              PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()))); 
              out.print("GET /" + m_index + " HTTP/1.1"); 
              out.print("\r\n");
              out.flush();
  
              BufferedReader input = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
              String line;
  
              while((line = input.readLine()) != null) {
                  System.out.println(line); //Gets stuck here
              }
          } catch (Exception e) {
              System.out.println("Unable to connect!");
              System.out.println("Be sure to enter correct URL and authentication.");
          }
          finally {
              System.exit(0);
          }
      }
  
      public static void setConnectionInfoFromArgs(String[] args) {
          String[] url = args[0].split("/");
          m_server = url[0];
          m_index = url[1];
  
          //Encode "userName:password" to Base64
          byte[] encodedBytes = Base64.getEncoder().encode(args[1].getBytes());
          m_authString = new String(encodedBytes);
      }
  
      private static void loginToServer() throws IOException {       
            DataOutputStream auth = new DataOutputStream(m_socket.getOutputStream());
            auth.writeBytes("Authorization: Basic " + m_authString);
      }
  }
  </code></pre>