日期:2013-10-26  浏览次数:20840 次

  今天,一个同事访问http://www.ibm.com.cn网站时,觉得很奇怪,明明键入的是http://www.ibm.com.cn,怎样IE自动把它变成了http://www.ibm.com/cn/,而且已访问结果也是正确的。他觉得很隐晦,IE难道有这么智能?

  只需了解HTTP协议,以上转换的原理很简单,其过程大致如下:

  1.为了从www.ibm.com.cn上取得网页,浏览器做了类似下面的事情。

telnet www.ibm.com.cn 80

GET / HTTP/1.1

Host: www.ibm.com.cn

Accept-Languge: zh_CN

这时WEB服务器上前往如下内容:

HTTP/1.1 301 Moved Permanently

Date: Thu, 16 Feb 2006 12:14:13 GMT

Server: IBM_HTTP_Server

Location: http://www.ibm.com/link/redirect.www.ibm.com/www.ibm.com.cn/

Content-Length: 268

Content-Type: text/html

<!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="http://www.ibm.com/link/redirect.www.ibm.com/www.ibm.com.cn/">here</a>.

</p>

</body>

</html>

  依据HTTP协议里的规定,3开头的错误码表示重定向,并通知浏览器从指定的URL重新获取文件。

  2.于是,浏览器又做了类似下面的事情。

telnet www.ibm.com 80

GET  /link/redirect.www.ibm.com/www.ibm.com.cn/

Host: www.ibm.com.cn

Accept-Languge: zh_CN

这时WEB服务器上前往如下内容:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head><title>302 Found</title></head><body>

<h1>Found</h1>

<p>The document has moved

<a href="http://www.ibm.com/cn/">here</a>.</p>

</body></html>

还是重定向。

  3.于是,浏览器又做了类似下面的事情。

telnet www.ibm.com 80

GET /cn/ HTTP/1.1
Host: www.ibm.com.cn

Accept-Languge: zh_CN

这回终于得到了网面真正的内容。

参考:RFC 2616中的错误码:

Status-Code    =
            "100"  ; Section 10.1.1: Continue
          | "101"  ; Section 10.1.2: Switching Protocols
          | "200"  ; Section 10.2.1: OK
          | "201"  ; Section 10.2.2: Created
          | "202"  ; Section 10.2.3: Accepted
          | "203"  ; Section 10.2.4: Non-Authoritative Information
          | "204"  ; Section 10.2.5: No Content
          | "205"  ; Section 10.2.6: Reset Content
          | "206"  ; Section 10.2.7: Partial Content
          | "300"  ; Section 10.3.1: Multiple Choices
          | "301"  ; Section 10.3.2: Moved Permanently
          | "302"  ; Section 10.3.3: Found
          | "303"  ; Section 10.3.4: See Other
          | "304"  ; Section 10.3.5: Not Modified
          | "305"  ; Section 10.3.6: Use Proxy
          | "307"  ; Section 10.3.8: Temporary Redirect