socket http read

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpTest {
    public static void main(String[] args) {
        socket_http_reade();
    }

    public static void socket_http_reade() {
        String hostname = "10.63.9.55";
        int port = 80;
        String httpPackage = getTempGetMethodPackage(hostname);
        StringBuilder httpRsp = new StringBuilder();
        Socket s = new Socket();
        try {
            s.connect(new InetSocketAddress(hostname, port), 1000);
            OutputStream out = s.getOutputStream();
            out.write(httpPackage.toString().getBytes("utf-8"));
            out.flush();

            InputStream in = s.getInputStream();
            int readed = 0;
            int contentLength = 0;
            while (true) {
                byte[] lineDatas = readHttpHeaderLine(in);
                readed += lineDatas.length;
                String line = new String(lineDatas, "utf-8");
                if (line.startsWith("Content-Length")) {
                    String[] tmp = line.split(":");
                    contentLength = Integer.parseInt(tmp[1].trim());
                }
                httpRsp.append("> " + line);
                if (line.equals("
")) {
                    break;
                }
            }

            byte[] bodys = new byte[contentLength];
            readed = 0;
            while (readed < contentLength) {
                byte b = (byte) in.read();
                bodys[readed] = b;
                readed++;
            }
            String body = new String(bodys, "utf-8");
            System.out.print(body);

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static byte[] readHttpHeaderLine(InputStream in) throws IOException {
        byte[] buf = new byte[200];
        int readed = 0;
        byte b;
        do {
            b = (byte) in.read();
            int bufLen = buf.length;
            if (readed > bufLen) {
                byte[] temp = new byte[bufLen * 2];
                for (int i = 0; i < bufLen; i++) {
                    temp[i] = buf[i];
                }
                buf = temp;
            } else {
                buf[readed] = b;
            }
            readed++;
        } while (((char) b) != '
');

        byte[] temp = new byte[readed];
        for (int i = 0; i < readed; i++) {
            temp[i] = buf[i];
        }

        return temp;
    }

    public static String getTempGetMethodPackage(String host) {
        StringBuilder httpPackage = new StringBuilder();
        httpPackage.append("GET / HTTP/1.1
");
        httpPackage.append("host: " + host + "
");
        httpPackage.append("Proxy-Connection: keep-alive");
        httpPackage.append("Cache-Control: max-age=0
");
        httpPackage.append("Upgrade-Insecure-Requests: 1
");
        httpPackage.append(
                "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36
");
        httpPackage.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
");
        httpPackage.append("Accept-Encoding: gzip, deflate, sdch
");
        httpPackage.append("Accept-Language: zh-CN,zh;q=0.8
");
        httpPackage.append("
");

        return httpPackage.toString();
    }

    
}
原文地址:https://www.cnblogs.com/byxxw/p/5981486.html