下面是获取头信息,做个备忘。
PHP代码:
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />/n"; } else { $out = "GET / HTTP/1.1"; $out .= "Host: www.example.com"; $out .= "Accept: */*"; fwrite($fp, $out); while (!feof($fp)) { $content.=fgets($fp, 128); } fclose($fp); echo $content; }
引用:
在Web开发的过程中,人们常常忽视了这样的一个事实:很多工作都是在底层完成的——是底层协议使得客户端浏览器与Web服务器之间的通信成为可能。学习通信的过程是值得的,因为你可以从中得到数量令人吃惊的有用信息。例如,你可以建立自适应的Web应用程序,它可以根据用户浏览器的性能得到最好的效果,而且你可以改变报头信息来把自定义页面传递到各种平台上,包括无线电话和PDA。
HTTP 请求头概述
HTTP 客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST)。如有必要,客户程序还可以选择发送其他的请求头。大多数请求头并不是必需的,但Content-Length除外。对于POST请求来说Content-Length必须出现。
下面是一些最常见的请求头:
Accept:浏览器可接受的MIME类型。
Accept-Charset:浏览器可接受的字符集。
Accept-Encoding:浏览器能够进行解码的数据编码方式,比如gzip。Servlet能够向支持gzip的浏览器返回经gzip编码的HTML页面。许多情形下这可以减少5到10倍的下载时间。
Accept-Language:浏览器所希望的语言种类,当服务器能够提供一种以上的语言版本时要用到。
Authorization:授权信息,通常出现在对服务器发送的WWW-Authenticate头的应答中。
Connection:表示是否需要持久连接。如果Servlet看到这里的值为“Keep-Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间。要实现这一点, Servlet需要在应答中发送一个Content-Length头,最简单的实现方法是:先把内容写入ByteArrayOutputStream,然后在正式写出内容之前计算它的大小。
Content-Length:表示请求消息正文的长度。
Cookie:这是最重要的请求头信息之一,参见后面《Cookie处理》一章中的讨论。
From:请求发送者的email地址,由一些特殊的Web客户程序使用,浏览器不会用到它。
Host:初始URL中的主机和端口。
If-Modified-Since:只有当所请求的内容在指定的日期之后又经过修改才返回它,否则返回304“Not Modified”应答。
Pragma:指定“no-cache”值表示服务器必须返回一个刷新后的文档,即使它是代理服务器而且已经有了页面的本地拷贝。
Referer:包含一个URL,用户从该URL代表的页面出发访问当前请求的页面。
User-Agent:浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用。
UA-Pixels,UA-Color,UA-OS,UA-CPU:由某些版本的IE浏览器所发送的非标准的请求头,表示屏幕大小、颜色深度、操作系统和CPU类型。
有关HTTP头完整、详细的说明,请参见 http://www.w3.org/Protocols/的HTTP规范。
5.XMLHttpRequest
=============================================================================
提供客户端同http服务器通讯的协议
返回值
长整形标准http状态码,定义如下:
| Number | Description |
| — | — |
|
100
|
Continue
|
|
101
|
Switching protocols
|
|
200
|
OK
|
|
201
|
Created
|
|
202
|
Accepted
|
|
203
|
Non-Authoritative Information
|
|
204
|
No Content
|
|
205
|
Reset Content
|
|
206
|
Partial Content
|
|
300
|
Multiple Choices
|
|
301
|
Moved Permanently
|
|
302
|
Found
|
|
303
|
See Other
|
|
304
|
Not Modified
|
|
305
|
Use Proxy
|
|
307
|
Temporary Redirect
|
|
400
|
Bad Request
|
|
401
|
Unauthorized
|
|
402
|
Payment Required
|
|
403
|
Forbidden
|
|
404
|
Not Found
|
|
405
|
Method Not Allowed
|
|
406
|
Not Acceptable
|
|
407
|
Proxy Authentication Required
|
|
408
|
Request Timeout
|
|
409
|
Conflict
|
|
410
|
Gone
|
|
411
|
Length Required
|
|
412
|
Precondition Failed
|
|
413
|
Request Entity Too Large
|
|
414
|
Request-URI Too Long
|
|
415
|
Unsupported Media Type
|
|
416
|
Requested Range Not Suitable
|
|
417
|
Expectation Failed
|
|
500
|
Internal Server Error
|
|
501
|
Not Implemented
|
|
502
|
Bad Gateway
|
|
503
|
Service Unavailable
|
|
504
|
Gateway Timeout
|
|
505
|
HTTP Version Not Supported
|
备注
长整形,此属性只读,返回当前请求的http状态码,此属性仅当数据发送并接收完毕后才可获取。
statusText
=======================================================================
返回当前请求的响应行状态
Authentication Required
|
|
408
|
Request Timeout
|
|
409
|
Conflict
|
|
410
|
Gone
|
|
411
|
Length Required
|
|
412
|
Precondition Failed
|
|
413
|
Request Entity Too Large
|
|
414
|
Request-URI Too Long
|
|
415
|
Unsupported Media Type
|
|
416
|
Requested Range Not Suitable
|
|
417
|
Expectation Failed
|
|
500
|
Internal Server Error
|
|
501
|
Not Implemented
|
|
502
|
Bad Gateway
|
|
503
|
Service Unavailable
|
|
504
|
Gateway Timeout
|
|
505
|
HTTP Version Not Supported
|
备注
长整形,此属性只读,返回当前请求的http状态码,此属性仅当数据发送并接收完毕后才可获取。
statusText
=======================================================================
返回当前请求的响应行状态