2010-06-14 94 views
7

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

例如,如果我想 “看看这是怎么回事”,我可以用的telnet做这样的事情:

... $ 远程登录nytimes.com 80

Trying 199.239.136.200... 
Connected to nytimes.com. 
Escape character is '^]'. 

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>  
... 

这是一个简单的方法来访问颇有些相关信息。

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

我该怎么做?

基本上,而不是得到一个HTTP/1.1 200好我想知道我怎么能得到301?

我知道我可以在浏览器中输入网址的名称,并“看到”我被重定向了,但我想知道什么工具可以用来真正“看到” 301重定向。

顺便说一句,我做了一个telnet测试,但是当我输入www.example.org,我重定向到example.org(没有www),我只能看到一个“200 OK”,我不喜欢看不到301.

回答

2

确定两分钟回答我找到了答案的问题...

执行以下操作不起作用:

telnet www.example.org 80 
GET/HTTP/1.0 
{enter} 
{enter} 

但以下工作正常:

telnet example.org 80 
GET/HTTP/1.0 
Host: www.example.org 
{enter} 
{enter} 

我的错误是通过www.example.orgtelnet(而不是example.org),然后不指定任何“主机:”

现在它的工作原理,我得到这个:

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. 

注:在Windows Vista/7,Telnet客户端是不是默认安装的。要安装它,按照指示在这里:Install Telnet Client - Microsoft TechNet

+0

这不适用于Windows 7 – Krunal 2012-09-07 05:46:48

1

Firefox插件HTTP Live headers对此非常有用。后

+0

啊有趣,感谢您的链接...但我一直在寻找更多的东西 “低层次”,如* telnet * :)有趣的是,我找到了一个方法来发布问题后两分钟让我的301 :) – NoozNooz42 2010-06-14 13:06:26

1

在头(远程登录响应),你会看到它的第一行:

HTTP/1.1 301 Moved Permanently 
Via: XXXXXXXXXXX 
Connection: close 
Proxy-Connection: close 
Content-Length: 0 
Date: Mon, 14 Jun 2010 13:03:14 GMT 
Location: /xxxxxxxxx 
Server: XXXXXXX 
Cache-Control: private 

感谢

+0

其实你不以我在问题中显示的方式,这是整点我的questi在:)看看我的答案,看看为什么它不是在我尝试使用telnet的方式工作,以及需要做些什么才能使其工作:) – NoozNooz42 2010-06-14 13:09:17

+0

您没有在第一行301,因为它wasn一个。如果“www”版本不是“非www”版本,它不是telnet问题,也不是我的答案。这是因为两个网址中的一个会这样做,而另一个则不会。这些是2个不同的URL。 – 2010-06-14 13:37:29

15

一个在我看来,更为方便的解决方法是使用卷曲。

只需运行:

$ curl -I http://example.com 

,它将返回的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/ 
1

一个测试它是指定一个301重定向目标网站上,并使用curl -L选项方式告诉你301重定向。

-L, --location 

(HTTP) If the server reports that the requested page has moved to a different 
location (indicated with a Location: header and a 3XX response code), this option 
will make curl redo the request on the new place. 

例如:

:~$ curl -IL mail.com 

你得到:

HTTP/1.1 301 Moved Permanently 
Date: Tue, 07 Feb 2017 13:17:12 GMT 
Server: Apache 
Location: https://www.mail.com/ 
Vary: Accept-Encoding 
Connection: close 
Content-Type: text/html; charset=iso-8859-1 

HTTP/1.1 302 Found 
Date: Tue, 07 Feb 2017 13:17:13 GMT 
Server: Apache 
Vary: X-Forwarded-Proto,Host 
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/ 
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/ 
Cache-Control: no-cache, no-store, must-revalidate 
Pragma: no-cache 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
Location: https://www.mail.com/int/ 
Content-Language: en-US 
Connection: close 

HTTP/1.1 200 OK 
Date: Tue, 07 Feb 2017 13:17:13 GMT 
Server: Apache 
Vary: X-Forwarded-Proto,Host,Accept-Encoding 
Set-Cookie: cookieKID=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/ 
Set-Cookie: cookiePartner=kid%40autoref%40mail.com; Domain=.mail.com; Expires=Thu, 09-Mar-2017 13:17:13 GMT; Path=/ 
Cache-Control: no-cache, no-store, must-revalidate 
Pragma: no-cache 
Expires: Thu, 01 Jan 1970 00:00:00 GMT 
Set-Cookie: JSESSIONID=964DA3BD186E264928A8C188E3BB919D; Path=/mailcom-webapp/; HttpOnly 
Content-Language: en-INT 
Content-Length: 74356 
Connection: close 
Content-Type: text/html;charset=UTF-8