Read a web page with Socket in Java

Description

The following code shows how to read a web page with Socket.

Example


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/*from w  w  w.j a  v a 2s . c o m*/
public class Main {
  public static void main(String args[]) throws IOException,
      UnknownHostException {
    String resource, host, file;
    int slashPos;

    resource = "http://www.java2s.com/index.htm".substring(7); // skip HTTP://
    slashPos = resource.indexOf('/'); 
    if (slashPos < 0) {
      resource = resource + "/";
      slashPos = resource.indexOf('/');
    }
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");
    MyHTTPConnection webConnection = new MyHTTPConnection(host);
    if (webConnection != null) {
      BufferedReader in = webConnection.get(file);
      String line;
      while ((line = in.readLine()) != null) { // read until EOF
        System.out.println(line);
      }
    }
    System.out.println("\nDone.");
  }

}
class MyHTTPConnection {
  public final static int HTTP_PORT = 80;

  InetAddress wwwHost;

  DataInputStream dataInputStream;

  PrintStream outputStream;

  public MyHTTPConnection(String host) throws UnknownHostException {
    wwwHost = InetAddress.getByName(host);
    System.out.println("WWW host = " + wwwHost);
  }

  public BufferedReader get(String file) throws IOException {
    Socket httpPipe;
    InputStream in;
    OutputStream out;
    BufferedReader bufReader;
    PrintWriter printWinter;
    httpPipe = new Socket(wwwHost, HTTP_PORT);
    if (httpPipe == null) {
      return null;
    }
    // get raw streams
    in = httpPipe.getInputStream();
    out = httpPipe.getOutputStream();
    // turn into useful ones
    bufReader = new BufferedReader(new InputStreamReader(in));
    printWinter = new PrintWriter(new OutputStreamWriter(out), true);
    if (in == null || out == null || bufReader == null || printWinter == null) {
      System.out.println("Failed to open streams to socket.");
      return null;
    }
    // send GET request
    System.out.println("GET " + file + " HTTP/1.0\n");
    printWinter.println("GET " + file + " HTTP/1.0\n");
    // read response until blank separator line
    String response;
    while ((response = bufReader.readLine()).length() > 0) {
      System.out.println(response);
    }
    return bufReader; 
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode