9

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

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

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

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

获取/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 OK ,而是想知道如何获得 301?

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

顺便说一句,我确实使用 telnet 进行了测试,但是当我输入 www.example.org 时,我将其重定向到 example.org(没有 www),我只能看到“200 OK”,我看不到301。

4

6 回答 6

19

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

只需运行:

$ 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/
于 2013-07-21T10:44:42.577 回答
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.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.

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

于 2010-06-14T13:04:33.617 回答
1

Firefox 插件HTTP Live 标头对此非常有用。

于 2010-06-14T13:01:00.607 回答
1

在(telnet 响应的)标题中,您将在第一行看到它:

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

谢谢

于 2010-06-14T13:04:39.920 回答
1

为此,我使用FirebugNet面板

替代文字

于 2010-06-14T13:04:54.423 回答
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
于 2017-02-07T13:18:19.373 回答