如何从http标头Java中检索特定信息?

时间:2019-03-24 12:55:49

标签: java sockets http header

我花了很多时间寻找解决方案,但找不到任何解决方案。我正在发送HTTP请求,需要从http标头中检索某些信息。我正在手动处理套接字库。如何省略其他信息,而仅从http标头中检索要显示的信息?有什么方法或要使用什么库,以便可以格式化http标头?

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;
import java.text.SimpleDateFormat;

public class socketv1 {

    public static void main(String[] args) throws Exception {
        InetAddress addr = InetAddress.getByName("www.google.com");
        Socket socket = new Socket(addr, 80);
        //socket.bind    (new InetSocketAddress (socket.getLocalAddress().getHostAddress(), 0));
        //socket.connect (new InetSocketAddress (socket.getInetAddress().getHostAddress(),   80), 1000);

        boolean autoflush = true;

        System.out.println("URL requested: " + socket.getInetAddress().getHostName());
        System.out.println("Client: " + socket.getLocalAddress().getHostAddress() + " " + socket.getLocalPort());
        System.out.println("Server: " + socket.getInetAddress().getHostAddress() + " " + socket.getPort());

        System.out.println("");

        PrintWriter out = new PrintWriter(socket.getOutputStream(), autoflush);
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


        // send an HTTP request to the web server
        out.println("GET / HTTP/1.1");
        out.println("Host: www.google.com:80");
        out.print ("Date Accessed: date" + "\r\n");
        out.println("Connection: Close");
        out.println();

        // read the response
        boolean loop = true;
        StringBuilder sb = new StringBuilder(8096);
        while (loop) {
            if (in.ready()) {
                int i = 0;
                while (i != -1) {
                    i = in.read();
                    sb.append((char) i);
                }
                loop = false;
            }
        }
        System.out.println(sb.toString());
        socket.close();
    }
}

预期:

URL requested: www.google.com

Client: 192.168.1.110 53954

Server: 172.217.167.68 80

Date Accessed: 24/03/2019 14:53:59 AEST

实际:


URL requested: www.google.com

Client: 192.168.1.110 53954

Server: 172.217.167.68 80

HTTP/1.1 200 OK

Date: Sun, 24 Mar 2019 12:52:16 GMT

Expires: -1

Cache-Control: private, max-age=0

Content-Type: text/html; charset=ISO-8859-1

P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."

Server: gws

X-XSS-Protection: 1; mode=block

X-Frame-Options: SAMEORIGIN

Set-Cookie: 1P_JAR=2019-03-24-12; expires=Tue, 23-Apr-2019 12:52:16 GMT; path=/; domain=.google.com

Set-Cookie: NID=179=E-IxZRjdPtqBWrSM-bdqfdYDdzPlEaC7gkdFKxYoGRJpBIdD__1ZQiVFPrSuoEqme-yBucdcczqMw_EOJaUpfuXYy1auuQWd1-AZQ6WKmQR_pz8kFZqemdm4Bc-yH0P1Zc7ODKWEmtHKpE3nT2kqIhwfp7pLZrYd3YGMrZFUwZs; expires=Mon, 23-Sep-2019 12:52:16 GMT; path=/; domain=.google.com; HttpOnly

Accept-Ranges: none

Vary: Accept-Encoding

Connection: close

1 个答案:

答案 0 :(得分:0)

您是否必须使用套接字,不能做类似的事情:

import java.net.URL;
import java.net.URLConnection;

public class socketv1 {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.google.com");
        URLConnection c = url.openConnection();
        System.out.println(c.getHeaderField("Content-Type"));
    }
}

那么从一开始就使用连接吗?