fsockopen與HTTP 1.1/HTTP 1.0

來源:互聯網
上載者:User

標籤:tar   power   div   send   handler   .net   2.2.3   tail   url   

在前面的例子中,HTTP請求資訊頭有些指定了 HTTP 1.1,有些指定了 HTTP/1.0,有些又沒有指定,那麼他們之間有什麼區別呢?

關於HTTP 1.1與HTTP 1.0的一些基本情況,可以參考下 HTTP 1.1的詳細介紹 。

我們先來看一下 fsockopen 不指定 HTTP 的情況:

function asyn_sendmail(){    $ip = ‘121.199.24.143‘;    $url = ‘/php/sock.php‘;    $fp = fsockopen($ip, 80, $errno, $errstr, 5);    if (!$fp)    {        echo "$errstr ($errno)<br />\n";    }    $end = "\r\n";    $input = "GET $url$end";    $input.="$end";    fputs($fp, $input);    $html = ‘‘;    while (!feof($fp))    {        $html.=fgets($fp);    }    fclose($fp);    writelog($html);    echo $html;}function writelog($message){    $path = ‘F:\log2.txt‘;    $handler = fopen($path, ‘w+b‘);    if ($handler)    {        $success = fwrite($handler, $message);        fclose($handler);    }}asyn_sendmail();

sock.php:

<?php    echo "Welcome to NowaMagic";?> 

程式輸出:

Welcome to NowaMagic

log2.txt 內容也是:

Welcome to NowaMagic

那些接下來再看看在標題加上 HTTP 1.1 的程式:

function asyn_sendmail(){    $ip = ‘121.199.24.143‘;    $url = ‘/php/sock.php‘;    $fp = fsockopen($ip, 80, $errno, $errstr, 5);    if (!$fp)    {        echo "$errstr ($errno)<br />\n";    }    $end = "\r\n";    $input = "GET $url HTTP/1.1$end";    //如果不加下面這一句,會返回一個http400錯誤           $input.="Host: $ip$end";        //如果不加下面這一句,請求會阻塞很久          $input.="Connection: Close$end";     $input.="$end";    fputs($fp, $input);    $html = ‘‘;    while (!feof($fp))    {        $html.=fgets($fp);    }    fclose($fp);    writelog($html);    echo $html;}function writelog($message){    $path = ‘F:\log.txt‘;    $handler = fopen($path, ‘w+b‘);    if ($handler)    {        $success = fwrite($handler, $message);        fclose($handler);    }}asyn_sendmail();

程式輸出:

HTTP/1.1 200 OKDate: Fri, 07 Feb 2014 13:50:14 GMTServer: Apache/2.2.3 (CentOS)X-Powered-By: PHP/5.3.3Vary: Accept-EncodingContent-Length: 21Connection: closeContent-Type: text/html; charset=UTF-8Welcome to NowaMagic 

留意到注釋:

//如果不加下面這一句,請求會阻塞很久      $input.="Connection: Close$end";     $input.="$end";

原因是什麼呢? 可以參考 fsockopen用feof讀取http響應內容的一些問題。

//如果不加下面這一句,會返回一個http400錯誤       $input.="Host: $ip$end";    

報400錯誤:

HTTP/1.1 400 Bad RequestDate: Fri, 07 Feb 2014 13:54:57 GMTServer: Apache/2.2.3 (CentOS)Content-Length: 305Connection: closeContent-Type: text/html; charset=iso-8859-1

使用http1.1串連,要加上Host請求表頭。

小結:

  • HTTP 1.0, Apache Web 服務器中 $input.="Connection: Close$end"; 與 $input.="Connection: Close$end" 可都不需要。
  • HTTP 1.0, Nginx Web 服務器中 $input.="Connection: Close$end"; 與 $input.="Connection: Close$end" 都必需。
  • HTTP 1.1, Apache Web 服務器中 $input.="Connection: Close$end"; 必須要,$input.="Connection: Close$end" 可不用。
  • HTTP 1.1, Nginx Web 服務器中 $input.="Connection: Close$end"; 與 $input.="Connection: Close$end" 都必需。

fsockopen與HTTP 1.1/HTTP 1.0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.