喜悦国际村 
» 游客:  注册 | 登录 | 搜索 | 统计 | 帮助

RSS 订阅当前论坛  

喜悦证交所已经关闭

上一主题 下一主题
     
标题: [问题] 如何实现模拟http登陆  
 
yongcncn
新手上路
Rank: 1



UID 130744
精华 0
积分 6
帖子 2
金钱 6 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2008-6-30
状态 离线
如何实现模拟http登陆

如题
用PHP如何实现模拟http登陆
下面的类好像不行


<?php
function readHTTPDigestAuthenticatedFile($host,$file,$username,$password)
{
   if (!$fp=fsockopen($host,80, $errno, $errstr, 15))
       return false;
      
   //first do the non-authenticated header so that the server
   //sends back a 401 error containing its nonce and opaque
   $out = "GET /$file HTTP/1.1\r\n";
       $out .= "Host: $host\r\n";
       $out .= "Connection: Close\r\n\r\n";

     fwrite($fp, $out);

   //read the reply and look for the WWW-Authenticate element
   while (!feof($fp))
   {
       $line=fgets($fp, 512);
      
       if (strpos($line,"WWW-Authenticate:")!==false)
           $authline=trim(substr($line,18));
   }
   
   fclose($fp);
      
   //split up the WWW-Authenticate string to find digest-realm,nonce and opaque values
   //if qop value is presented as a comma-seperated list (e.g auth,auth-int) then it won't be retrieved correctly
   //but that doesn't matter because going to use 'auth' anyway
   $authlinearr=explode(",",$authline);
   $autharr=array();
   
   foreach ($authlinearr as $el)
   {
       $elarr=explode("=",$el);
       //the substr here is used to remove the double quotes from the values
       $autharr[trim($elarr[0])]=substr($elarr[1],1,strlen($elarr[1])-2);
   }
   
   foreach ($autharr as $k=>$v)
       echo("$k ==> $v\r\n");
   
   //these are all the vals required from the server
   $nonce=$autharr['nonce'];
   $opaque=$autharr['opaque'];
   $drealm=$autharr['Digest realm'];
   
   //client nonce can be anything since this authentication session is not going to be persistent
   //likewise for the cookie - just call it MyCookie
   $cnonce="sausages";
   
   //calculate the hashes of A1 and A2 as described in RFC 2617
   $a1="$usernamedrealmpassword";$a2="GET:/$file";
   $ha1=md5($a1);$ha2=md5($a2);
   
   //calculate the response hash as described in RFC 2617
   $concat = $ha1.':'.$nonce.':00000001:'.$cnonce.':auth:'.$ha2;
   $response=md5($concat);
   
   //put together the Authorization Request Header
   $out = "GET /$file HTTP/1.1\r\n";
       $out .= "Host: $host\r\n";
   $out .= "Connection: Close\r\n";
   $out .= "Cookie: cookie=MyCookie\r\n";
   $out .= "Authorization: Digest username=\"$username\", realm=\"$drealm\", qop=\"auth\", algorithm=\"MD5\", uri=\"/$file\", nonce=\"$nonce\", nc=00000001, cnonce=\"$cnonce\", opaque=\"$opaque\", response=\"$response\"\r\n\r\n";
   
   if (!$fp=fsockopen($host,80, $errno, $errstr, 15))
       return false;
   
   fwrite($fp, $out);
   
   //read in a string which is the contents of the required file
   while (!feof($fp))
   {
       $str.=fgets($fp, 512);
   }
   
   fclose($fp);
   
   return $str;
}

?>

[ 本帖最后由 yongcncn 于 2008-7-4 08:55 PM 编辑 ]


 附件: 您所在的用户组无法下载或查看附件
2008-7-4 08:52 PM#1
查看资料  发短消息  顶部
 
jtql420 (披着羊皮的狼)
金牌会员
Rank: 6Rank: 6
披着羊皮的狼


UID 70611
精华 0
积分 1968
帖子 450
金钱 1968 喜悦币
威望 0
人脉 0
阅读权限 70
注册 2006-3-29
来自 深圳
状态 离线
[推荐阅读] 哎, 我的一邻居炒 "中石油" 成了百万富翁!!
1、snoopy
2、pear中的http_client
3、ZF中的http_client

[ 本帖最后由 jtql420 于 2008-7-5 03:40 PM 编辑 ]




披着羊皮的狼 http://jtql420.cn
2008-7-5 03:39 PM#2
查看资料  访问主页  发短消息  QQ . .   顶部
 
lyxcf
中级会员
Rank: 3Rank: 3



UID 78421
精华 0
积分 309
帖子 205
金钱 309 喜悦币
威望 0
人脉 0
阅读权限 30
注册 2006-8-16
来自 北京 角门北路
状态 离线
[推荐阅读] 口袋里还有6元钱明天怎么办?
自己写的发送类
演示地址:http://www.shanxihu.com/send.php




网页搜索、图片搜索、音乐搜索、尽在搜集齐www.sojiqi.com
2008-7-5 06:17 PM#3
查看资料  访问主页  发短消息  QQ . .   顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题 | 开通个人空间  


 




Powered by Discuz! 6.1.0  © 2001-2010 Comsenz Inc.
Processed in 0.028230 second(s), 6 queries

(冀ICP备05009913号) 管理员:sadly 邮箱/MSN: sadly@phpx.com QQ:824008(长隐) 清除 Cookies - - Archiver - WAP