我正在尝试通过一个简单的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请求到在上面的回复中确定的Location值,我会得到status 404作为回复。我使用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我使用GET方法获取结果的方式是否有误?
发布于 2020-09-10 01:15:26
您正在向端口80发送一个纯文本请求,因此您尝试的URL实际上是http://tools.ietf.org/html/rfc3986
响应告诉您改为请求https://tools.ietf.org/html/rfc3986。这不是同一服务器上的不同路径,而是一个完整的URL。
不同之处在于,它以https开头,这意味着您需要在端口443上建立一个TLS安全的连接。
使用简单的netcat是不可能的,所以最好使用curl或wget这样的HTTP客户端
https://stackoverflow.com/questions/63816380
复制相似问题