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

问题描述 投票:0回答:1

我花了很多时间寻找解决方案但却找不到任何解决方案。我正在发送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
java sockets http header
1个回答
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"));
    }
}

所以从一开始就使用连接?

© www.soinside.com 2019 - 2024. All rights reserved.