HTTP GET请求始终返回301

时间:2020-09-09 17:09:39

标签: http

我正在尝试通过一个简单的GET请求从IETF网站获取一些RFC的HTML转储。但是,它以状态码301响应。我正在使用netcat通过以下命令来模拟HTTP GET请求:

$ printf 'GET /html/rfc3986 HTTP/1.1\r\nHost: tools.ietf.org\r\nConnection: close\r\n\r\n' | nc tools.ietf.org 80

由于上述命令而获得以下答复:

HTTP/1.1 301 Moved Permanently
Date: Wed, 09 Sep 2020 15:36:36 GMT
Server: Apache/2.2.22 (Debian)
Location: https://tools.ietf.org/html/rfc3986
Vary: Accept-Encoding
Content-Length: 323
Connection: close
Content-Type: text/html; charset=iso-8859-1
X-Pad: avoid browser bug

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://tools.ietf.org/html/rfc3986">here</a>.</p>
<hr>
<address>Apache/2.2.22 (Debian) Server at tools.ietf.org Port 80</address>
</body></html>

但是,如果我尝试向上述回复中确定的HTTP/1.0值发送基于HEAD的{​​{1}}请求,则会得到状态404的回复。我使用Location方法只是为了检查回复的状态码。

命令:

HEAD

回复:

printf 'HEAD https://tools.ietf.org/html/rfc3986 HTTP/1.0\r\n\r\n' | nc tools.ietf.org 80

我使用HTTP/1.1 404 Not Found Date: Wed, 09 Sep 2020 16:32:18 GMT Server: Apache/2.2.22 (Debian) Vary: accept-language,accept-charset,Accept-Encoding Accept-Ranges: bytes Connection: close Content-Type: text/html; charset=iso-8859-1 Content-Language: en Expires: Wed, 09 Sep 2020 16:32:18 GMT 方法获取结果的方式是否有误?

1 个答案:

答案 0 :(得分:2)

您正在向端口80发送纯文本请求,因此您尝试的URL实际上是http://tools.ietf.org/html/rfc3986

响应告诉您改为请求https://tools.ietf.org/html/rfc3986。这不是同一台服务器上的其他路径,而是完整的URL。

区别在于它以https开头,这意味着您需要在端口443上建立TLS保护的连接。

琐碎地使用netcat不可能做到这一点,因此最好使用curl或wget之类的HTTP客户端