首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTTP GET请求始终返回301

HTTP GET请求始终返回301
EN

Stack Overflow用户
提问于 2020-09-10 01:09:39
回答 1查看 40关注 0票数 0

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

代码语言:javascript
复制
$ printf 'GET /html/rfc3986 HTTP/1.1\r\nHost: tools.ietf.org\r\nConnection: close\r\n\r\n' | nc tools.ietf.org 80

执行上述命令后,会得到以下回复:

代码语言:javascript
复制
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.0HEAD请求到在上面的回复中确定的Location值,我会得到status 404作为回复。我使用HEAD方法只是为了检查回复的状态代码。

命令:

代码语言:javascript
复制
printf 'HEAD https://tools.ietf.org/html/rfc3986 HTTP/1.0\r\n\r\n' | nc tools.ietf.org 80

回复:

代码语言:javascript
复制
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方法获取结果的方式是否有误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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客户端

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63816380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档