码迷,mamicode.com
首页 > Web开发 > 详细

HTTP(一)

时间:2017-06-14 02:36:08      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:代理服务器   服务器   comm   error   code   微软   return   float   gets   

HTTP(一)

HTTP请求:请求行、消息报头、请求正文。格式如下:

Method Request-URI HTTP-Veraion CRLF

参数说明

  • Method 请求方法
  • Request-URI 一个统一资源标识符
  • HTTP-Version 请求的HTTP协议版本
  • CRLF 回车和换行

响应:状态行、消息报头、响应正文

HTTP-Version Status-Code Reason-Phrase CRLF

参数说明

  • HTTP-Verson 服务器HTTP协议的版本
  • Status-Code 服务器发回的响应状态码
  • Reson-Phrase 状态代码的文本描述
  • CRLF 回车和换行

响应状态码:

  1. 1XX:指示信息——请求已接收、已处理
  2. 2XX:成功——请求已被成功接收、理解、接收
  3. 3XX:重定向——要完成请求必须进行更进一步的操作
  4. 4XX:客户端错误——请求有语法错误或请求无法实现
  5. 5XX:服务端错误——服务端未能实现合法的请求

常见状态码

  • 200 OK:客户端请求成功
  • 400 Bad Request:客户端请求有语法错误,不被服务器所理解
  • 401 Unauthorize:请求未经授权,此状态码必须和WWW Authenticate 报头域一起使用
  • 403 Forbidden:服务器收到请求,但是拒绝提供服务
  • 404 Not Found:请求资源不存在
  • 500 Internal Server Error :服务器发生不可预期的错误
  • 503 Server Unavailable:服务器当前不能处理客户端的请求,一段时间后可能恢复正常

相关函数:

get_headers(string $url [, int $format = 0 ] ) — 取得服务器响应一个 HTTP 请求所发送的所有标头

返回包含有服务器响应一个 HTTP 请求所发送标头的索引或关联数组,如果失败则返回 FALSE 。 (通过判断状态码是否为200,就可以判断请求的资源存在与否)

telnet模拟请求

  1. cmd ->telnet localhost(域名) 80
  2. 按下crtl+] ,仔按下enter (打开回显功能)
3. 发送报文
GET /test/httptest.php?id=1 HTTP/1.1
Connection: close 
Host: localhost
Content-type:application/x-www-form-urlencoded
content-length:20
(两个回车)
#通过1.1版本协议请求index.html页面;connection: close是实用短连接,即服务器返回后就断开连接;Host字段知名页面所在的主机名。

测试服务端 httptest.php

<?php

	$dataGet = $_GET;
	$dataPost = $_POST;
	$dataGetstr = http_build_query($dataGet);   
	$dataPoststr = http_build_query($dataPost);

	if( $dataGet ){
		echo ‘get ‘.$dataGetstr;
	}

	if( $dataPost ){
			echo ‘post ‘.$dataPoststr;
	}
?>

PHP实现HTTP请求

$postData =http_build_query(
 array(
    ‘title‘  =>   "这里是 file_get_contents 提交的数据",
    ‘content‘ => "你好 !",
    ‘type‘ => 1
    )
);

#file_get_contents
$opts  = array(
   ‘http‘ =>array(
     ‘method‘ => "POST" ,
     ‘header‘ => "Host:localhost\r\n".
                "Content-type:application/x-www-form-urlencoded\r\n".
                "Content-length:".(strlen($postData))."\r\n".
               "Cookie: foo=bar\r\n",
     "content" => $postData,
      //‘timeout‘ => 60 * 60 // 超时时间(单位:s)
   )
);
 
$context  =  stream_context_create ( $opts );//创建数据流上下文

//file_get_contents( ‘http://localhost/test/httptest.php‘,false,$context );


#fopen
$postData = http_build_query(
array(
    ‘title‘  =>   "这里是 fopen 提交的数据",
    ‘content‘ => "你好 !",
    ‘type‘ => 1
));
$fp = fopen( ‘http://localhost/test/httptest.php‘,‘r‘,false,$context );

  • stream_context_create 创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。

curl方式提交

$url = "http://localhost/test/httptest.php";
$postData = http_build_query(
array(
    ‘title‘  =>   "这里是 fopen 提交的数据",
    ‘content‘ => "你好 !",
    ‘type‘ => 1
));
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL,$url  );
curl_setopt( $ch,CURLOPT_POST,1  );
curl_setopt( $ch,CURLOPT_POSTFIELDS,$postData  );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER,1  );
curl_exec();
curl_close();

socket方式提交

$postData = http_build_query(
array(
    ‘title‘  =>   "这里是 fopen 提交的数据",
    ‘content‘ => "你好 !",
    ‘type‘ => 1
));
$fp = fsockopen( "localhost",80,$errno,$errorStr,5 );
$request = "POST http://localhost/test/httptest.php HTTP/1.1\r\n";
$request .= "Host:localhost\r\n";
$request .= "Content-type:application/x-www-form-urlencoded\r\n";
$request .= "Content-length:".(strlen($postData))."\r\n";
$request .= $postData;
fwrite( $fp,$request  );

while(!feof($fp)){
    echo fgets($fp,1024);
}
fclose($fp);

-- 这段时间读读书,理解理解原理,生活也很充实。

HTTP(一)

标签:代理服务器   服务器   comm   error   code   微软   return   float   gets   

原文地址:http://www.cnblogs.com/followyou/p/7004502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!