2

我正在尝试使用 curl 登录到一个安全的 aspx 站点,并检索一些帐户的数据。
该页面使用 aspx __VIEWSTATE 来跟踪浏览器的状态。从这里检查请求标头是顺序:

用户从 Login.aspx(包括 __VIEWSTATE)GETS
用户 POSTS __VIEWSTATE、loginName 和 loginPassword 到 login.aspx -> 服务器响应 302 用户GETS Submissions.aspx

submits.aspx 是由 __EVENTTARGET=dgrdSubmissions$ctl0x$ctl00 引用的不同客户端的表,其中第一个 $ctl0x 代表该客户端的行。

用户POSTS_VIEWSTATE、 _EVENTTARGET 和一个 AdvisorView 参数到 submits.aspx -> 服务器响应 302 用户GETS Policy.aspx

这在浏览器中运行良好(Chrome - 该网站在 Firefox 中出现可疑中断并显示消息:引发了“System.Web.HttpUnhandledException”类型的异常),但在我的 php 脚本中,GET Policy.aspx 使用登录页面而不是预期响应客户信息。

这是我的代码(减去错误检查和页面显示):
Helper Functions:

function curl_page($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data=curl_exec($ch);
curl_close($ch);
return $data;
}

function curl_ssl_page($url="",$postdata=""){
$ch = curl_init();
$cookie = 'cookie.txt';
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch);

return $result;
}

function curl_get_page($url=""){

$ch = curl_init();
$cookie = 'cookie.txt';
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

$result = curl_exec ($ch);
return $result;
}

页面

Pages - Login:

if(isset($_POST['user-name'])) {
    //GET login page
    $url = "http://www.gryphinonline.ca/Login.aspx";
    $login_page = $this->curl_page($url);

    // get viewstate
    $regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i';
    $regexEventVal  = '/__EVENTVALIDATION\" value=\"(.*)\"/i';

$viewstate = $this->regexExtract($login_page,$regexViewstate,1);
$eventval = $this->regexExtract($login_page, $regexEventVal,1);

//Post to login page
$postdata = '__VIEWSTATE='.rawurlencode($viewstate)
     .'&txtLoginName='.$_POST['user-name']
     .'&txtPassword='.$_POST['password']
     .'&Start=Login+%2F+Ouverture+de+session';

$this->curl_ssl_page($url,$postdata);
header("Location:http://url-edited/submissions");
}

Pages - Submissions:
    $url = "http://www.gryphinonline.ca/Submissions.aspx";
$submissions = $this->curl_get_page($url);

$dom = new DOMDocument();
@$dom->loadHTML($submissions);

// scrape for data including viewstate
$view = $dom->getElementById('dgrdSubmissions'); 
if(!$view) header("Location://url-edited/login");

    $h_data = $dom->getElementsByTagName('div');
$h_data = $h_data->item(0);

if(isset($_POST['__EVENTTARGET'])){
    $postdata=array();
    foreach ($_POST as $key => $value) {
        $postdata[]=$key.'='.$value;
    }
    $postdata = implode('&', $postdata);

    $this->curl_ssl_page($url,$postdata);
    header("Location:http://url-edited/policy");        
}

Pages - Policy:
    $url = "http://www.gryphinonline.ca/Policy.aspx";
$policy = $this->curl_get_page($url);

据我所知,所有的 HTTP 请求和 cookie 都是相同的。有人知道这里发生了什么吗?这可能与该网站的 Firefox 问题有关,还是我误解了一些基本的东西?

我已经在这几天了,任何帮助将不胜感激。

4

1 回答 1

1

原来我忘记将 POST 字符串进行 urlencode 提交。

于 2012-06-02T18:55:40.407 回答