HTTP标准的响应头

该代码片段展示了一个简单的Java程序,它使用Socket连接到百度服务器并发送一个HTTPGET请求。服务器返回一个302Found响应,包含重定向到www.baidu.com的Location头部信息以及其他cookies和元数据。程序读取并打印出服务器的响应内容。
摘要由CSDN通过智能技术生成

HTTP/1.1 302 Found

Connection: keep-alive

Content-Length: 154

Content-Type: text/html

Date: Fri, 24 Mar 2023 16:41:39 GMT

Location: https://www.baidu.com/

P3p: CP=" OTI DSP COR IVA OUR IND COM "

P3p: CP=" OTI DSP COR IVA OUR IND COM "

Server: BWS/1.1

Set-Cookie: BAIDUID=BD63BC4CD2C7E8C44A83E1A0D47DA11B:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: BIDUPSID=BD63BC4CD2C7E8C44A83E1A0D47DA11B; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: PSTM=1679676099; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com

Set-Cookie: BAIDUID=BD63BC4CD2C7E8C43CD9850878EC4787:FG=1; max-age=31536000; expires=Sat, 23-Mar-24 16:41:39 GMT; domain=.baidu.com; path=/; version=1; comment=bd

Set-Cookie: BD_LAST_QID=14433188352113219423; path=/; Max-Age=1

Traceid: 1679676099060692148214433188352113219423

X-Frame-Options: sameorigin

X-Ua-Compatible: IE=Edge,chrome=1

<html>

<head><title>302 Found</title></head>

<body bgcolor="white">

<center><h1>302 Found</h1></center>

<hr><center>nginx</center>

</body>

</html>


package com.qfedu.http.response;

import java.io.*;
import java.net.Socket;

public class MyBrowser {
    public static void main(String[] args) throws IOException {
        //将上一个案例中浏览器发送给MyHttpServer的http请求内容,发送给百度

        //向百度服务器发送请求
        Socket socket = new Socket("www.baidu.com", 80);
        System.out.println(socket);
        //2,通过连接中的输出流将HTTP请求内容发送给百度
        OutputStream outputStream = socket.getOutputStream();
        PrintWriter out = new PrintWriter(outputStream);
        out.println("GET / HTTP/1.1");
        out.println("Host: www.baidu.com:80");
        out.println("Connection: keep-alive");
        out.println("sec-ch-ua: \"Microsoft Edge\";v=\"111\", \"Not(A:Brand\";v=\"8\", \"Chromium\";v=\"111\"");
        out.println("sec-ch-ua-mobile: ?0");
        out.println("sec-ch-ua-platform: \"Windows\"");
        out.println("Upgrade-Insecure-Requests: 1");
        out.println("User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.51");
        out.println("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        out.println("Sec-Fetch-Site: none");
        out.println("Sec-Fetch-Mode: navigate");
        out.println("Sec-Fetch-User: ?1");
        out.println("Sec-Fetch-Dest: document");
        out.println("Accept-Encoding: gzip, deflate, br");
        out.println("Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        out.println("");
        out.flush();
        //3,通过输入流接收百度的相应数据
        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String responseContent = null;
        while((responseContent = reader.readLine()) != null) {
            System.out.println(responseContent);
        }
    }
}

我们通过获取的请求头发送给百度然后百度就会给我们响应

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeForWater

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值