gpt4 book ai didi

java - 尝试使用原始套接字发送 GET 请求时收到 301

转载 作者:行者123 更新时间:2023-12-01 19:49:32 28 4
gpt4 key购买 nike

为什么我尝试访问的每个 URL 都会收到 301 错误?我只是想使用原始套接字作为练习!我已在下面列出了答复。堆栈溢出使我输入更多单词,因为我的响应主要是代码。所以这里还有一些话

    Socket s = new Socket(InetAddress.getByName("stackoverflow.com"), 80);
PrintWriter pw = new PrintWriter(s.getOutputStream());
pw.println("GET / HTTP/1.1\r");
pw.println("Host: stackoverflow.com\r");
pw.println("\r");

pw.flush();

BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String t;
while ((t = br.readLine()) != null)
System.out.println(t);
br.close();

HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/
X-Request-Guid: 5d0b1d72-5b10-4aa2-a563-4fd3d6b155cb
Content-Security-Policy: upgrade-insecure-requests
Content-Length: 143
Accept-Ranges: bytes
Date: Mon, 20 Aug 2018 06:19:15 GMT
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: cache-dfw18637-DFW
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1534745955.491886,VS0,VE37
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=0bf5c623-b16b-d4b6-e822-504daf42c16e;
domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT;
path=/; HttpOnly

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://stackoverflow.com/">here</a>.</h2>
</body></html>

最佳答案

HTTP 服务器想要重定向客户端以使用 HTTPS。现在很难找到知名的 HTTP 服务器,那是不行的。

尝试例如“jquery.com”,它不会重定向到 HTTPS。

顺便说一句:“原始套接字”对于 TCP 套接字来说不是正确的术语。通常术语“原始套接字”用于原始 IP 套接字 (SOCK_RAW)

关于java - 尝试使用原始套接字发送 GET 请求时收到 301,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51925038/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com