zoukankan      html  css  js  c++  java
  • HTTP(一)

    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
    ".
                    "Content-type:application/x-www-form-urlencoded
    ".
                    "Content-length:".(strlen($postData))."
    ".
                   "Cookie: foo=bar
    ",
         "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
    ";
    $request .= "Host:localhost
    ";
    $request .= "Content-type:application/x-www-form-urlencoded
    ";
    $request .= "Content-length:".(strlen($postData))."
    ";
    $request .= $postData;
    fwrite( $fp,$request  );
    
    while(!feof($fp)){
        echo fgets($fp,1024);
    }
    fclose($fp);
    

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

  • 相关阅读:
    PHP环境搭建-修改密码
    先挖个坑....
    usaco 1.2.1(指针技巧)
    warfare(最大生成树裸题)
    最大生成树(最小生成树同理)
    快排
    简单邻接表代码实现
    并查集模板题
    并查集
    get 新技能
  • 原文地址:https://www.cnblogs.com/followyou/p/7004502.html
Copyright © 2011-2022 走看看