集册 Java实例教程 通过SocketChannel获取HTTP

通过SocketChannel获取HTTP

欢马劈雪     最近更新时间:2020-01-02 10:19:05

742
HTTP通过SocketChannel获取


import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.SocketAddress;/* from n o w j a v a . c o m - 时  代  Java*/

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();

    }


    private void trace() {

        WritableByteChannel destination = null;

        final String host = "www.google.co.kr";/*来自 时   代    Java - nowjava.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 / 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) {

                    res
展开阅读全文