HTTP協議分析系列(六)------php+socket+cookie請求

www.verycd.com爲例

在火狐瀏覽器登錄wuming88888888賬號爲發送方

chrome瀏覽器登錄wuming1990賬號爲接收方

分析發送方的表單

分析提交頁源代碼POST的數據

<?php 
require('./http.class.php');
$http=new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0');
$msg=array(
	'formhash'=>'10fe754a',
	'message'=>'你好',
	'pmsubmit'=>true,
	'pmsubmit_btn'=>'發送',
	'refer'=>'http://home.verycd.com/space.php?do=pm&filter=privatepm',
	'username'=>'wuming1990'
);
file_put_contents('./res.html',$http->post($msg));

?>

打開res.html,分析源代碼

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 05 Dec 2014 06:57:05 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Set-Cookie: sid=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: member_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: member_name=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: pass_hash=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: rememberme=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: mgroupId=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: coppa=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: uchome_auth=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: uchome_loginuser=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Location: http://www.verycd.com/account/profile/
Set-Cookie: uchome__refer=cp.php%253Fac%253Dprofile; path=/; domain=.verycd.com

33fc

調試查看自己發送的是什麼內容?

這時候我們可以分析出錯誤出現在第一行
打印該對象
Http Object
(
    [errno:protected] => 0
    [errstr:protected] => 
    [response:protected] => 
    [url:protected] => Array
        (
            [scheme] => http
            [host] => home.verycd.com
            [path] => /cp.php
            [query] => ac=pm&op=send&touid=0&pmid=0
            [port] => 80
        )

    [version:protected] => HTTP/1.1
    [fh:protected] => Resource id #3
    [line:protected] => Array
        (
            [0] => POST /cp.php HTTP/1.1
        )

    [header:protected] => Array
        (
            [0] => Host:home.verycd.com
            [1] => Content-type:application/x-www-form-urlencoded
            [2] => Content-length:185
        )

    [body:protected] => Array
        (
            [0] => formhash=10fe754a&message=%E4%BD%A0%E5%A5%BD&pmsubmit=1&pmsubmit_btn=%E5%8F%91%E9%80%81&refer=http%3A%2F%2Fhome.verycd.com%2Fspace.php%3Fdo%3Dpm%26filter%3Dprivatepm&username=wuming1990
        )

)

修改我們的http類

<pre name="code" class="php"> //http請求類的接口
interface Proto{
	//連接url
	function conn($url);
	//發送get查詢
	function get();
	//發送post查詢
	function post();
	//關閉連接
	function close();
}
class Http implements Proto{
	const CRLF="\r\n";
	protected $errno=-1;
	protected $errstr='';
	protected $response='';
	protected $url=null;
	protected $version='HTTP/1.1';
	protected $fh=null;
	protected $line=array();
	protected $header=array();
	protected $body=array();
	
	public function __construct($url){
		$this->conn($url);
		$this->setHeader('Host:'.$this->url['host']);
	}
	//此方法負責寫請求行
	protected function setLine($method){
		$this->line[0]=$method.' '.$this->url['path'].'?'.$this->url['query'].' '.$this->version;
	}
	//此方法負責寫頭信息
	public function setHeader($headerline){
		$this->header[]=$headerline;
	}
	//此方法負責寫主體信息
	protected function setBody($body){
		
		$this->body[]=http_build_query($body);;
	}
	//連接url
	function conn($url){
		$this->url=parse_url($url);
		//判斷端口
		if(!isset($this->url['port'])){
			$this->url['port']=80;
		}
		$this->fh=fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);
	}
	//構造get請求的數據
	function get(){
		$this->setLine('GET');
		$this->request();
		return $this->response;
	}
	//構造post請求的數據
	function post($body=array()){
		//構造主體信息
		$this->setLine('POST');
		
		//設置content-type
		$this->setHeader('Content-type:application/x-www-form-urlencoded');
		//設置主體信息,比GET不一樣的地方
		$this->setBody($body);
		//計算content-length
		$this->setHeader('Content-length:'.strlen($this->body[0]));
		$this->request();
		return $this->response;
	}
	//真正請求
	function request(){
		//把請求行,頭信息,實體信息  放在一個數組裏,便於拼接
		$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
		$req=implode(self::CRLF,$req);
// 		print_r($this);
// 		echo $req;
// 		exit;
		fwrite($this->fh,$req);
		
		while(!feof($this->fh)){
			$this->response.=fread($this->fh,1024);
		}
		
		$this->close();//關閉連接
		return $this->response;
	}
	//關閉連接
	function close(){
		fclose($this->fh);
	}
}




生成如下POST /cp.php?ac=pm&op=send&touid=0&pmid=0 HTTP/1.1Host:home.verycd.comContent-type:application/x-www-form-urlencodedContent-length:185formhash=10fe754a&message=%E4%BD%A0%E5%A5%BD&pmsubmit=1&pmsubmit_btn=%E5%8F%91%E9%80%81&refer=http%3A%2F%2Fhome.verycd.com%2Fspace.php%3Fdo%3Dpm%26filter%3Dprivatepm&username=wuming1990


HTTP/1.1 200 OK
Server: nginx
Date: Fri, 05 Dec 2014 07:11:39 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Set-Cookie: sid=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: member_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: member_name=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: pass_hash=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: rememberme=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: mgroupId=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: coppa=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: uchome_auth=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: uchome_loginuser=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.verycd.com
Set-Cookie: uchome__refer=cp.php%253Fac%253Dpm; path=/; domain=.verycd.com

表示已成功,但是不完全,我們接着看res.html中的內容
在網頁中有如下內容:表明需要先登錄以後才能操作


服務器怎麼知道咱們沒登陸的?

http一個很重要的特點:無狀態,兩次請求之間沒有關係。

服務器如何記住一個客戶?

 

建立cookie.php

<?php
header('content-type:text/html;charset=utf8'); 
setcookie('user','zhangsan');
echo '服務器給你的編號是zhangsan';
?>

建立readcookie.php

<?php
header('content-type:text/html;charset=utf8'); 
echo '服務器給你的編號是'.$_COOKIE['user'];
?>

利用命令窗口提交請求

增加提交的信息

<?php 
require('./http.class.php');
$http=new Http('http://home.verycd.com/cp.php?ac=pm&op=send&touid=0&pmid=0');
$http->setHeader('cookie:Hm_lvt_c7849bb40e146a37d411700cb7696e46=1417760419; Hm_lpvt_c7849bb40e146a37d411700cb7696e46=1417760610; post_action=repost; sid=7fd8c62c8d000561d658c4e25eccb6f791a8d4b6; member_id=9933070; member_name=wuming88888888; mgroupId=93; pass_hash=263b6d67494b1888f1e7b8cc227ea4bd; rememberme=true; uchome_auth=63a2o4ZG8YsPG1Tv4%2FIYiydpKrQVqgKgxAQgp%2FI5ZxYQIVjc8ad40VEyW2peEmnKYwKQ2qserNpgSOrxwXLKpDomid%2Fq; uchome_loginuser=wuming88888888; CNZZDATA1479=cnzz_eid%3D407399210-1417756656-http%253A%252F%252Fwww.verycd.com%252F%26ntime%3D1417756656; __utma=248211998.394160120.1417760633.1417760633.1417760633.1; __utmb=248211998.8.10.1417760633; __utmc=248211998; __utmz=248211998.1417760633.1.1.utmcsr=verycd.com|utmccn=(referral)|utmcmd=referral|utmcct=/account/profile/base/; __utmt=1; uchome_sendmail=1; uchome_checkpm=1; dcm=1');
$msg=array(
	'formhash'=>'10fe754a',
	'message'=>'i am from wuming88888888',
	'pmsubmit'=>true,
	'pmsubmit_btn'=>'發送',
	'refer'=>'http://home.verycd.com/space.php?do=pm&filter=privatepm',
	'username'=>'wuming1990'
);
file_put_contents('./res.html',$http->post($msg));
echo 'ok';
?>

再看wuming1990的用戶是否收到信息



PS:如果發送不成功,表明COOKIE值不對,cookie的生成與請求頭信息有關,保守做法:把全部請求頭信息加到post請求裏面)

Accepttext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Encoding gzip, deflate

Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3

Connection keep-alive

CookieHm_lvt_c7849bb40e146a37d411700cb7696e46=1417760419; Hm_lpvt_c7849bb40e146a37d411700cb7696e46=1417760610; post_action=repost; sid=7fd8c62c8d000561d658c4e25eccb6f791a8d4b6; member_id=9933070; member_name=wuming88888888; mgroupId=93; pass_hash=263b6d67494b1888f1e7b8cc227ea4bd; rememberme=true; uchome_auth=63a2o4ZG8YsPG1Tv4%2FIYiydpKrQVqgKgxAQgp%2FI5ZxYQIVjc8ad40VEyW2peEmnKYwKQ2qserNpgSOrxwXLKpDomid%2Fq; uchome_loginuser=wuming88888888; CNZZDATA1479=cnzz_eid%3D407399210-1417756656-http%253A%252F%252Fwww.verycd.com%252F%26ntime%3D1417756656; __utma=248211998.394160120.1417760633.1417760633.1417760633.1; __utmb=248211998.8.10.1417760633; __utmc=248211998; __utmz=248211998.1417760633.1.1.utmcsr=verycd.com|utmccn=(referral)|utmcmd=referral|utmcct=/account/profile/base/; __utmt=1; uchome_sendmail=1; uchome_checkpm=1; dcm=1

Hosthome.verycd.com

Refererhttp://home.verycd.com/cp.php?ac=pm

User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0

 


書寫格式:

$http->setHeader('紅色字體:黑色字體');



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章