Read a web page with SocketChannel in Java

Description

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

Example


import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
/*from   ww w . j  a v  a2  s.c o m*/
public class Main {
  public static void main(String args[]) throws Exception {
    String resource, host, file;
    int slashPos;

    resource = "www.java2s.com/index.htm";
    slashPos = resource.indexOf('/'); // find host/file separator
    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 + "'");

    SocketChannel channel = null;

    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharsetEncoder encoder = charset.newEncoder();

    ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
    CharBuffer charBuffer = CharBuffer.allocate(1024);

    InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
    channel = SocketChannel.open();
    channel.connect(socketAddress);

    String request = "GET " + file + " \r\n\r\n";
    channel.write(encoder.encode(CharBuffer.wrap(request)));

    while ((channel.read(buffer)) != -1) {
      buffer.flip();
      decoder.decode(buffer, charBuffer, false);
      charBuffer.flip();
      System.out.println(charBuffer);
      buffer.clear();
      charBuffer.clear();
    }
    System.out.println("\nDone.");
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode