如何测试HTTP 301重定向?

Noo*_*z42 7 http telnet http-status-codes http-status-code-301

如何轻松测试HTTP返回码,比如301重定向?

例如,如果我想"看看发生了什么",我可以使用telnet做这样的事情:

... $ telnet nytimes.com 80

Trying 199.239.136.200...
Connected to nytimes.com.
Escape character is '^]'.
Run Code Online (Sandbox Code Playgroud)

GET/HTTP/1.0

(输入)

(输入)

HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Mon, 14 Jun 2010 12:18:04 GMT
Content-type: text/html
Set-cookie: RMID=007af83f42dd4c161dfcce7d; expires=Tuesday, 14-Jun-2011 12:18:04 GMT; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Set-cookie: adxcs=-; path=/; domain=.nytimes.com
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Cache-control: no-cache
Pragma: no-cache
Connection: close

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>    
<head>      
...
Run Code Online (Sandbox Code Playgroud)

这是访问相当一些信息的简单方法.

但现在我想测试301重定向确实是301重定向.

我怎么能这样做?

基本上,我不想获得HTTP/1.1 200,而是想知道如何获得301?

我知道我可以在浏览器中输入URL的名称并"看到"我被重定向,但我想知道可以使用哪些工具来实际"真正"看到301重定向.

顺便说一下,我用telnet进行了测试,但是当我进入www.example.org时,我重定向到example.org(没有www),我只能看到"200 OK",我看不到301.

vpe*_*son 17

在我看来,一个更方便的解决方案是使用Curl.

简单地运行:

$ curl -I http://example.com
Run Code Online (Sandbox Code Playgroud)

它将像这样返回HTTP标头

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.1.19
Date: Sun, 21 Jul 2013 10:41:47 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://other.example.com/
Run Code Online (Sandbox Code Playgroud)


Noo*_*z42 3

好吧,回答问题两分钟后我找到了答案......

执行以下操作不起作用:

telnet www.example.org 80
GET / HTTP/1.0
{enter}
{enter}
Run Code Online (Sandbox Code Playgroud)

但以下效果很好:

telnet example.org 80
GET / HTTP/1.0
Host: www.example.org
{enter}
{enter}
Run Code Online (Sandbox Code Playgroud)

我的错误是将www.example.org传递给telnet(而不是example.org),然后不指定任何"Host: "

现在它可以工作了,我明白了:

Connected to xxx.xx
Escape character is '^]'.
GET / HTTP/1.0
Host: www.example.org

HTTP/1.1 301 Moved Permanently
Server: Apache-Coyote/1.1
Location: http://example.org/
Connection: close
Date: Mon, 14 Jun 2010 13:02:22 GMT
Connection: close

Connection closed by foreign host.
Run Code Online (Sandbox Code Playgroud)

注意:在 Windows Vista/7 上,默认情况下不安装 Telnet 客户端。要安装它,请按照此处的说明进行操作:安装 Telnet 客户端 - Microsoft TechNet