尝试使用原始套接字发送GET请求时获取301

时间:2018-08-20 06:19:31

标签: java sockets http

为什么我尝试访问的每个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>

1 个答案:

答案 0 :(得分:3)

HTTP服务器希望重定向客户端以使用HTTPS。 如今,很难找到众所周知的HTTP服务器,而不会这样做。

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

BTW:“原始套接字”不是TCP套接字的正确术语。 通常,术语“原始套接字”用于原始IP套接字(SOCK_RAW)

相关问题