hadoop概述-包括各个组件

各种成熟的消息队列MQ

  返回  

用Java原生Socket解析HTTP的请求头

2021/8/20 14:06:22 浏览:

想用Java原生的Socket捕捉一下HTTP报文,下面是测试代码。

public class HttpServer {


    public static void main(String[] args){
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        try {
            serverSocket = new ServerSocket(80);
            accept = serverSocket.accept();

            inputStream = accept.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String s;
            while ((s = bufferedReader.readLine()) != null) {
                System.out.println(s);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(inputStream != null) {
                    inputStream.close();
                }

                if(accept != null) {
                    accept.close();
                }

                if(serverSocket != null) {
                    serverSocket.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

启动程序后,浏览器访问地址(postman访问无效,连接后又主动断开了,原因未知):

localhost:80/hello/world 

控制台打印信息如下:

注释版

GET /hello/world HTTP/1.1      #请求方式,路径,协议版本
Host: localhost                #请求的主机ip
Connection: keep-alive         #连接后不断开,可在服务器设置保持连接的时间
sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"  #浏览器信息
sec-ch-ua-mobile: ?0  #移动端信息
Upgrade-Insecure-Requests: 1   #与http相关
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36  #用户代理,提供客户端浏览器、操作系统等信息
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9  #表示客户端能够接收的数据类型
Sec-Fetch-Site: none               #Sec-Fetch-* 更细粒度的划分客户端,方便服务器做访问控制
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br          #申明浏览器支持的编码类型
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8    #浏览器支持的语言

       

 

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号