狀態碼用來告訴HTTP客戶端,HTTP服務器是否產生了預期的Response。javascript
HTTP/1.1中定義了5類狀態碼, 狀態碼由三位數字組成,第一個數字定義了響應的類別php
PHP在響應報文中添加狀態碼示例:css
<?php public function response_code(){ http_code(303); } function http_code($num){ $http = array ( 100 => "HTTP/1.1 100 Continue", 101 => "HTTP/1.1 101 Switching Protocols", 200 => "HTTP/1.1 200 OK", 201 => "HTTP/1.1 201 Created", 202 => "HTTP/1.1 202 Accepted", 203 => "HTTP/1.1 203 Non-Authoritative Information", 204 => "HTTP/1.1 204 No Content", 205 => "HTTP/1.1 205 Reset Content", 206 => "HTTP/1.1 206 Partial Content", 300 => "HTTP/1.1 300 Multiple Choices", 301 => "HTTP/1.1 301 Moved Permanently", 302 => "HTTP/1.1 302 Found", 303 => "HTTP/1.1 303 See Other", 304 => "HTTP/1.1 304 Not Modified", 305 => "HTTP/1.1 305 Use Proxy", 307 => "HTTP/1.1 307 Temporary Redirect", 400 => "HTTP/1.1 400 Bad Request", 401 => "HTTP/1.1 401 Unauthorized", 402 => "HTTP/1.1 402 Payment Required", 403 => "HTTP/1.1 403 Forbidden", 404 => "HTTP/1.1 404 Not Found", 405 => "HTTP/1.1 405 Method Not Allowed", 406 => "HTTP/1.1 406 Not Acceptable", 407 => "HTTP/1.1 407 Proxy Authentication Required", 408 => "HTTP/1.1 408 Request Time-out", 409 => "HTTP/1.1 409 Conflict", 410 => "HTTP/1.1 410 Gone", 411 => "HTTP/1.1 411 Length Required", 412 => "HTTP/1.1 412 Precondition Failed", 413 => "HTTP/1.1 413 Request Entity Too Large", 414 => "HTTP/1.1 414 Request-URI Too Large", 415 => "HTTP/1.1 415 Unsupported Media Type", 416 => "HTTP/1.1 416 Requested range not satisfiable", 417 => "HTTP/1.1 417 Expectation Failed", 500 => "HTTP/1.1 500 Internal Server Error", 501 => "HTTP/1.1 501 Not Implemented", 502 => "HTTP/1.1 502 Bad Gateway", 503 => "HTTP/1.1 503 Service Unavailable", 504 => "HTTP/1.1 504 Gateway Time-out" ); header($http[$num]); } ?>
HTTP Response Header包含如下內容:html
做用:生成消息的具體時間和日期java
例如:Date: Sat, 11 Feb 2012 11:35:14 GMTweb
做用:瀏覽器會在指定過時時間內使用本地緩存json
例如:Expires: Tue, 08 Feb 2022 11:35:14 GMT跨域
做用:瀏覽器
例如:Vary: Accept-Encoding緩存
做用:用於跨域設置Cookie, 這樣能夠解決iframe跨域訪問cookie的問題
例如:P3P: CP=CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR
做用:很是重要的header, 用於把cookie 發送到客戶端瀏覽器, 每個寫入cookie都會生成一個Set-Cookie.
例如:Set-Cookie: sc=4c31523a; path=/; domain=.acookie.taobao.com
做用:和If-None-Match 配合使用。 (實例請看上節中If-None-Match的實例)
例如:ETag: 「03f2b33c0bfcc1:0」
做用:用於指示資源的最後修改日期和時間。(實例請看上節的If-Modified-Since的實例)
例如:Last-Modified: Wed, 21 Dec 2011 09:09:10 GMT
做用:WEB服務器告訴瀏覽器本身響應的對象的類型和字符集,
例如:
Content-Type: text/html; charset=utf-8
Content-Type:text/html;charset=GB2312
Content-Type: image/jpeg
做用: 指明實體正文的長度,以字節方式存儲的十進制數字來表示。在數據下行的過程當中,Content-Length的方式要預先在服務器中緩存全部數據,而後全部數據再一古腦兒地發給客戶端。
須要說明的是,Content-Length由response 報文 body實際長度肯定。
例如:Content-Length: 19847
做用: WEB服務器告訴瀏覽器本身響應的對象的語言者
例如: Content-Language:da
做用:指明HTTP服務器的軟件信息,內容由實際服務器肯定
例如:Server: Apache/2.4.12 (Win32) OpenSSL/1.0.1l PHP/5.6.8
做用:表示網站是用什麼技術開發的
例如:X-Powered-By: PHP/5.6.8
例如:Connection: keep-alive 當一個網頁打開完成後,客戶端和服務器之間用於傳輸HTTP數據的TCP鏈接不會關閉,若是客戶端再次訪問這個服務器上的網頁,會繼續使用這一條已經創建的鏈接
例如:Connection: close 表明一個Request完成後,客戶端和服務器之間用於傳輸HTTP數據的TCP鏈接會關閉, 當客戶端再次發送Request,須要從新創建TCP鏈接。
WEB服務器代表本身使用了什麼壓縮方法(gzip,deflate)壓縮響應中的對象。
例如:Content-Encoding:gzip
做用: 用於重定向一個新的位置, 包含新的URL地址
PHP 發送 Response報文就要簡單多了,以下片斷所示:
//發送Response Header header("Content-Type:image/jpeg"); //發送Response body echo "body 內容";
#附:HTTP header 詳解
Requests部分
Header | 解釋 | 示例 |
---|---|---|
Accept | 指定客戶端可以接收的內容類型 | Accept: text/plain, text/html |
Accept-Charset | 瀏覽器能夠接受的字符編碼集。 | Accept-Charset: iso-8859-5 |
Accept-Encoding | 指定瀏覽器能夠支持的web服務器返回內容壓縮編碼類型。 | Accept-Encoding: compress, gzip |
Accept-Language | 瀏覽器可接受的語言 | Accept-Language: en,zh |
Accept-Ranges | 能夠請求網頁實體的一個或者多個子範圍字段 | Accept-Ranges: bytes |
Authorization | HTTP受權的受權證書 | Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
Cache-Control | 指定請求和響應遵循的緩存機制 | Cache-Control: no-cache |
Connection | 表示是否須要持久鏈接。(HTTP 1.1默認進行持久鏈接) | Connection: close |
Cookie | HTTP請求發送時,會把保存在該請求域名下的全部cookie值一塊兒發送給web服務器。 | Cookie: $Version=1; Skin=new; |
Content-Length | 請求的內容長度 | Content-Length: 348 |
Content-Type | 請求的與實體對應的MIME信息 | Content-Type: application/x-www-form-urlencoded |
Date | 請求發送的日期和時間 | Date: Tue, 15 Nov 2010 08:12:31 GMT |
Expect | 請求的特定的服務器行爲 | Expect: 100-continue |
From | 發出請求的用戶的Email | From: user@email.com |
Host | 指定請求的服務器的域名和端口號 | Host: www.zcmhi.com |
If-Match | 只有請求內容與實體相匹配纔有效 | If-Match: 「737060cd8c284d8af7ad3082f209582d」 |
If-Modified-Since | 若是請求的部分在指定時間以後被修改則請求成功,未被修改則返回304代碼 | If-Modified-Since: Sat, 29 Oct 2010 19:43:31 GMT |
If-None-Match | 若是內容未改變返回304代碼,參數爲服務器先前發送的Etag,與服務器迴應的Etag比較判斷是否改變 | If-None-Match: 「737060cd8c284d8af7ad3082f209582d」 |
If-Range | 若是實體未改變,服務器發送客戶端丟失的部分,不然發送整個實體。參數也爲Etag | If-Range: 「737060cd8c284d8af7ad3082f209582d」 |
If-Unmodified-Since | 只在實體在指定時間以後未被修改才請求成功 | If-Unmodified-Since: Sat, 29 Oct 2010 19:43:31 GMT |
Max-Forwards | 限制信息經過代理和網關傳送的時間 | Max-Forwards: 10 |
Pragma | 用來包含實現特定的指令 | Pragma: no-cache |
Proxy-Authorization | 鏈接到代理的受權證書 | Proxy-Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== |
Range | 只請求實體的一部分,指定範圍 | Range: bytes=500-999 |
Referer | 先前網頁的地址,當前請求網頁緊隨其後,即來路 | Referer: http://www.zcmhi.com/archives/71.html |
TE | 客戶端願意接受的傳輸編碼,並通知服務器接受接受尾加頭信息 | TE: trailers,deflate;q=0.5 |
Upgrade | 向服務器指定某種傳輸協議以便服務器進行轉換(若是支持) | Upgrade: HTTP/2.0, SHTTP/1.3, IRC/6.9, RTA/x11 |
User-Agent | User-Agent的內容包含發出請求的用戶信息 | User-Agent: Mozilla/5.0 (Linux; X11) |
Via | 通知中間網關或代理服務器地址,通訊協議 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning | 關於消息實體的警告信息 | Warn: 199 Miscellaneous warning |
Header | 解釋 | 示例 |
---|---|---|
Accept-Ranges | 代表服務器是否支持指定範圍請求及哪一種類型的分段請求 | Accept-Ranges: bytes |
Age | 從原始服務器到代理緩存造成的估算時間(以秒計,非負) | Age: 12 |
Allow | 對某網絡資源的有效的請求行爲,不容許則返回405 | Allow: GET, HEAD |
Cache-Control | 告訴全部的緩存機制是否能夠緩存及哪一種類型 | Cache-Control: no-cache |
Content-Encoding | web服務器支持的返回內容壓縮編碼類型。 | Content-Encoding: gzip |
Content-Language | 響應體的語言 | Content-Language: en,zh |
Content-Length | 響應體的長度 | Content-Length: 348 |
Content-Location | 請求資源可替代的備用的另外一地址 | Content-Location: /index.htm |
Content-MD5 | 返回資源的MD5校驗值 | Content-MD5: Q2hlY2sgSW50ZWdyaXR5IQ== |
Content-Range | 在整個返回體中本部分的字節位置 | Content-Range: bytes 21010-47021/47022 |
Content-Type | 返回內容的MIME類型 | Content-Type: text/html; charset=utf-8 |
Date | 原始服務器消息發出的時間 | Date: Tue, 15 Nov 2010 08:12:31 GMT |
ETag | 請求變量的實體標籤的當前值 | ETag: 「737060cd8c284d8af7ad3082f209582d」 |
Expires | 響應過時的日期和時間 | Expires: Thu, 01 Dec 2010 16:00:00 GMT |
Last-Modified | 請求資源的最後修改時間 | Last-Modified: Tue, 15 Nov 2010 12:45:26 GMT |
Location | 用來重定向接收方到非請求URL的位置來完成請求或標識新的資源 | Location: http://www.zcmhi.com/archives/94.html |
Pragma | 包括實現特定的指令,它可應用到響應鏈上的任何接收方 | Pragma: no-cache |
Proxy-Authenticate | 它指出認證方案和可應用到代理的該URL上的參數 | Proxy-Authenticate: Basic |
refresh | 應用於重定向或一個新的資源被創造,在5秒以後重定向(由網景提出,被大部分瀏覽器支持) |
Refresh: 5; url=
http://www.zcmhi.com/archives/94.html
|
Retry-After | 若是實體暫時不可取,通知客戶端在指定時間以後再次嘗試 | Retry-After: 120 |
Server | web服務器軟件名稱 | Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) |
Set-Cookie | 設置Http Cookie | Set-Cookie: UserID=JohnDoe; Max-Age=3600; Version=1 |
Trailer | 指出頭域在分塊傳輸編碼的尾部存在 | Trailer: Max-Forwards |
Transfer-Encoding | 文件傳輸編碼 | Transfer-Encoding:chunked |
Vary | 告訴下游代理是使用緩存響應仍是從原始服務器請求 | Vary: * |
Via | 告知代理客戶端響應是經過哪裏發送的 | Via: 1.0 fred, 1.1 nowhere.com (Apache/1.1) |
Warning | 警告實體可能存在的問題 | Warning: 199 Miscellaneous warning |
WWW-Authenticate | 代表客戶端請求實體應該使用的受權方案 | WWW-Authenticate: Basic |
//定義編碼 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/atom+xml'); //CSS header('Content-type: text/css'); //Javascript header('Content-type: text/javascript'); //JPEG Image header('Content-type: image/jpeg'); //JSON header('Content-type: application/json'); //PDF header('Content-type: application/pdf'); //RSS header('Content-Type: application/rss+xml; charset=ISO-8859-1'); //Text (Plain) header('Content-type: text/plain'); //XML header('Content-type: text/xml'); //200 OK header('HTTP/1.1 200 OK'); //設置一個404頭: header('HTTP/1.1 404 Not Found'); //設置地址被永久的重定向 header('HTTP/1.1 301 Moved Permanently'); //轉到一個新地址 header('Location: http://www.example.org/'); //文件延遲轉向: header('Refresh: 10; url=http://www.example.org/'); print 'You will be redirected in 10 seconds'; //固然,也可使用html語法實現 //<meta http-equiv="refresh" content="10;http://www.example.org/ /> //override X-Powered-By: PHP: header('X-Powered-By: PHP/4.4.0'); header('X-Powered-By: Brain/0.6b'); //文檔語言 header('Content-language: en'); //告訴瀏覽器最後一次修改時間 $time = time() - 60; // or filemtime($fn), etc header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT'); //告訴瀏覽器文檔內容沒有發生改變 header('HTTP/1.1 304 Not Modified'); //設置內容長度 header('Content-Length: 1234'); //設置爲一個下載類型 header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="example.zip"'); header('Content-Transfer-Encoding: binary'); //load the file to send: readfile('example.zip'); //對當前文檔禁用緩存 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Pragma: no-cache'); //設置內容類型: header('Content-Type: text/html; charset=iso-8859-1'); header('Content-Type: text/html; charset=utf-8'); header('Content-Type: text/plain'); //純文本格式 header('Content-Type: image/jpeg'); //JPG*** header('Content-Type: application/zip'); // ZIP文件 header('Content-Type: application/pdf'); // PDF文件 header('Content-Type: audio/mpeg'); // 音頻文件 header('Content-Type: application/x-shockw**e-flash'); //Flash動畫 //顯示登錄對話框 header('HTTP/1.1 401 Unauthorized'); header('WWW-Authenticate: Basic realm="Top Secret"'); print 'Text that will be displayed if the user hits cancel or '; print 'enters wrong login data';
// 調用請求函數
publicfunction test(){ $chost ='test'; $cpath ='http://www.cleey.com'; $crequest =''; $rel = $this->http_post($crequest, $chost, $cpath,80); var_dump($rel);}// 模擬http post請求function http_post($request, $host, $path, $port){ $strlen = strlen($request); $http_request ="POST $path HTTP/1.0\r\n"; $http_request .="Accept-Encoding: gzip,deflate\r\n"; $http_request .="Content-Type: application/json;charset=UTF-8\r\n"; $http_request .="User-Agent: Jakarta Commons-HttpClient/3.1\r\n"; $http_request .="Host: www.cleey.com\r\n"; $http_request .="Content-Length: $strlen\r\n";// $http_request .= "Token: wwwww\r\n"; $http_request .="\r\n"; $http_request .= $request; $response ='';if(false!=( $fs =@fsockopen( $host, $port, $errno, $errstr,10))){ fwrite( $fs, $http_request );while(!feof( $fs )) $response .= fgets( $fs,1160);// One TCP-IP packet fclose( $fs ); $response = explode("\r\n\r\n", $response,2);}return $response;}
HTTP報文 客戶端和Web服務器在一次HTTP事務中交換的消息被稱爲HTTP報頭,客戶端發送給服務器的請求消息被稱爲請求報文,服務器返回給客戶端的響應消息被稱爲響應報頭。請求報文和響應報頭採用純文本編碼,由一行行簡單的字符串組成。一個完整的HTTP報文由以下三個部分構成。 起始行:表明HTTP報文的第一行文字,請求報文利用起始行表示採用的HTTP方法、請求URI和採用的HTTP版本,而響應報文的起始行在承載着HTTP版本和響應狀態碼等信息。 報頭集合:HTTP報文的起始行後面能夠包含零個或者多個報頭字段。每一個報頭表現爲一個鍵/值對,鍵和值分別表示報頭名稱和報頭的值,二者經過冒號(「:」)進行分割。HTTP報文采用一個空行做爲報頭集合結束的標誌。 主體內容:表明報頭集合結束標誌的空行以後就是HTTP報文的主體部分了。客戶端提交給服務器的數據通常置於請求報頭的主體,而響應報頭的主體也承載着服務器返回給客戶端的數據。不管是請求報文仍是響應報文,其主體部分均是能夠缺省的。 接下來咱們看看一個具體HTTP報文具備怎樣的結構。下面這個文本片斷反映的是咱們經過Chrome瀏覽器訪問微軟的官網(www.microsoft. com)對應的HTTP請求,起始行體現了HTTP請求的三個基本屬性,即HTTP方法(GET)、目標資源(http://www.microsoft.com/en-us/default.aspx)和協議版本(HTTP/1.1)。 1: GET http://www.microsoft.com/en-us/default.aspx HTTP/1.1 2: Host: www.microsoft.com 3: Connection: keep-alive 4: Cache-Control: max-age=0 5: User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7 6: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 7: Accept-Encoding: gzip,deflate,sdch 8: Accept-Language: en-US,en;q=0.8 9: Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 10: Cookie: ... 上述這個請求報文不具備主體,因此起始行以外的全部內容均爲報頭集合,咱們們能夠根據這些報頭得到主機名稱、採用的緩存策略、瀏覽器相關信息、以及客戶端支持的媒體類型(Media Type)、編碼方式、語言和字符集等。 前面的HTTP請求經過瀏覽器發送給服務端以後會接收到具備以下結構的響應報文,咱們能夠此從它的起始行獲得採用的HTTP版本(HTTP/1.1)和響應狀態碼(「200 OK」,表示請求被正常接收處理)。響應的內容被封裝到響應報文的主體部分,其媒體類型的經過報頭「Content-Type」表示。因爲該響應報文的主體內容是一個HTML文檔,因此「Content-Type」報頭表示的媒體類型爲「text/html」。 1: HTTP/1.1 200 OK 2: Cache-Control: no-cache 3: Pragma: no-cache 4: Content-Type: text/html; charset=utf-8 5: Content-Encoding: gzip 6: Expires: -1 7: Vary: Accept-Encoding 8: Server: Microsoft-IIS/7.5 9: X-AspNet-Version: 2.0.50727 10: VTag: 791897542300000000 11: P3P: CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI" 12: X-Powered-By: ASP.NET 13: Date: Wed, 18 Jan 2012 07:06:25 GMT 14: Content-Length: 34237 15: 16: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 17: <html>…</html> [1]超文本/超媒體(HyperText/HyperMedia):超文本是一份呈現文本內容的電子文檔,其核心在於能夠利用內嵌的「超連接(Hyperlink)」直接訪問引用的另外一份文檔。超媒體對超文本做了簡單的擴展以呈現多媒體內容(好比圖片、音頻和視頻等)。HTML文檔是咱們常見的最爲典型的超文本/超媒體文件。 [2] 除了採用這種4個層次的劃分方法以外,還具備另外兩種典型的劃分方式。其中一種在鏈路層下面添加一個基於物理網絡硬件的物理層,這種劃分方法與此沒有本質的區別。另一種則是將TCP/IP協議簇劃分爲包括應用層、表示層、會話層、傳輸層、網絡層、鏈路層和物理層在內的7個層次。