HTTP get via SocketChannel - Java Network

Java examples for Network:Socket Channel

Description

HTTP get via SocketChannel

Demo Code


import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.SocketChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;

public class HttpGet {

    public static final void main(String[] args) {

        new HttpGet().trace();
    }/*from w  ww  . j a  v a2  s  . c  om*/

    private void trace() {
        WritableByteChannel destination = null;
        final String host = "www.google.co.kr";
        final int port = 80;
        String path = "/";

        SocketAddress serverAddress = new InetSocketAddress(host, port);
        try (SocketChannel server = SocketChannel.open(serverAddress)) {
            server.configureBlocking(true);

            String request = "GET / HTTP/1.1\r\n" + "Host: " + host
                    + "\r\n" + "Accept: *" + "\r\n" + "\r\n";
            System.out.println("request message: " + request);

            CharBuffer requestChars = CharBuffer.wrap(request);
            Charset charset = Charset.forName("ISO-8859-1");
            ByteBuffer requesttBytes = charset.encode(requestChars);

            server.write(requesttBytes);

            destination = Channels.newChannel(System.out);
            ByteBuffer data = ByteBuffer.allocateDirect(32 * 1024);

            boolean headers = true;
            int responseCode = -1;
            while (server.read(data) != -1) {
                data.flip();

                if (responseCode == -1) {
                    responseCode = 100 * (data.get(9) - '0') + 10
                            * (data.get(10) - '0') + 1
                            * (data.get(11) - '0');

                }

                if (responseCode < 200 || responseCode >= 300) {
                    System.err.println("HTTP Error: " + responseCode);
                }

                if (!headers) {
                    try {
                        for (;;) {
                            if ((data.get() == 13) && (data.get() == 10)
                                    && (data.get() == 13)
                                    && (data.get() == 10)) {
                                break;
                            }
                        }
                    } catch (BufferUnderflowException e) {
                        data.position(data.position() - 3);
                        data.compact();
                        continue;
                    }
                }

                while (data.hasRemaining()) {
                    destination.write(data);
                }

                data.clear();
            }

        } catch (IOException e) {
            System.err.println(e);
            System.err.println("Usage: java HttpGet ");

        } finally {

        }

    }

    private void options() {
        WritableByteChannel destination = null;
        final String host = "www.google.co.kr";
        final int port = 80;
        String path = "/";

        SocketAddress serverAddress = new InetSocketAddress(host, port);
        try (SocketChannel server = SocketChannel.open(serverAddress)) {
            server.configureBlocking(true);

            String request = "OPTIONS / HTTP/1.1\r\n" + "Host: " + host
                    + "\r\n" + "\r\n";
            System.out.println("request message: " + request);

            CharBuffer requestChars = CharBuffer.wrap(request);
            Charset charset = Charset.forName("ISO-8859-1");
            ByteBuffer requesttBytes = charset.encode(requestChars);

            server.write(requesttBytes);

            destination = Channels.newChannel(System.out);
            ByteBuffer data = ByteBuffer.allocateDirect(32 * 1024);

            boolean headers = true;
            int responseCode = -1;
            while (server.read(data) != -1) {
                data.flip();

                if (responseCode == -1) {
                    responseCode = 100 * (data.get(9) - '0') + 10
                            * (data.get(10) - '0') + 1
                            * (data.get(11) - '0');

                }

                if (responseCode < 200 || responseCode >= 300) {
                    System.err.println("HTTP Error: " + responseCode);
                }

                if (!headers) {
                    try {
                        for (;;) {
                            if ((data.get() == 13) && (data.get() == 10)
                                    && (data.get() == 13)
                                    && (data.get() == 10)) {
                                break;
                            }
                        }
                    } catch (BufferUnderflowException e) {
                        data.position(data.position() - 3);
                        data.compact();
                        continue;
                    }
                }

                while (data.hasRemaining()) {
                    destination.write(data);
                }

                data.clear();
            }

        } catch (IOException e) {
            System.err.println(e);
            System.err.println("Usage: java HttpGet ");

        } finally {

        }

    }

    private void getHttp() {
        WritableByteChannel destination = null;
        final String host = "www.google.com";
        final int port = 80;
        String path = "/";

        SocketAddress serverAddress = new InetSocketAddress(host, port);
        try (SocketChannel server = SocketChannel.open(serverAddress)) {
            server.configureBlocking(true);

            String request = "GET " + path + " HTTP/1.1\r\n" + "Host: "
                    + host + "\r\n" + "\r\n";
            System.out.println("request message: " + request);

            CharBuffer requestChars = CharBuffer.wrap(request);
            Charset charset = Charset.forName("ISO-8859-1");
            ByteBuffer requesttBytes = charset.encode(requestChars);

            server.write(requesttBytes);

            destination = Channels.newChannel(System.out);
            ByteBuffer data = ByteBuffer.allocateDirect(32 * 1024);

            boolean headers = true;
            int responseCode = -1;
            while (server.read(data) != -1) {
                data.flip();

                if (responseCode == -1) {
                    responseCode = 100 * (data.get(9) - '0') + 10
                            * (data.get(10) - '0') + 1
                            * (data.get(11) - '0');

                }

                if (responseCode < 200 || responseCode >= 300) {
                    System.err.println("HTTP Error: " + responseCode);
                }

                if (!headers) {
                    try {
                        for (;;) {
                            if ((data.get() == 13) && (data.get() == 10)
                                    && (data.get() == 13)
                                    && (data.get() == 10)) {
                                break;
                            }
                        }
                    } catch (BufferUnderflowException e) {
                        data.position(data.position() - 3);
                        data.compact();
                        continue;
                    }
                }

                while (data.hasRemaining()) {
                    destination.write(data);
                }

                data.clear();
            }

        } catch (IOException e) {
            System.err.println(e);
            System.err.println("Usage: java HttpGet ");

        } finally {

        }

    }
}

Related Tutorials