尝试使用PHP和cURL登录ASPX网站

时间:2013-08-23 11:39:57

标签: php asp.net curl

几天后,我正在尝试使用cURL登录特定网站。

该网站是用ASP.net编写的,我使用的是PHP 5.x。

这是我迄今为止尝试过的但没有成功:

    $url = "https://intouch.techdata.com/default.aspx?countrycode=&CssStyleSheet=default.css&CultureCode=&local_url=&MPSearchId=&Culture=&cos=";
    $cookie_file_path = 'cookies.txt';
    $agent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36";

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);         
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); 
    curl_setopt($ch, CURLOPT_POSTREDIR, 2);
    curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    curl_setopt($ch, CURLOPT_POST, 1); 

    $post_data['__VIEWSTATE'] = "/wEPDwUKLTg1MDYwODIwOA9kFgRmDxYCHgRocmVmBRcvaW1hZ2VzL2RlZmF1bHQuY3NzP3Y9M2QCAQ9kFhgCAw8PFgIeBFRleHQFKldlbGNvbWUgdG8gdGhlIE9ubGluZSBQcm9kdWN0IENhdGFsb2d1ZSBvZmRkAgUPDxYCHwEFIVJlZ2lzdGVyZWQgdXNlcnMgZW
    50ZXIgPGJyPiBoZXJlOmRkAgsPFgIeB1Zpc2libGVoFgICAQ8PFgIfAQUJUGFzc3dvcmQ6ZGQCDQ8PZBYCHgxhdXRvY29tcGxldGUFA29mZmQCDw8PFgIfAQUPQWNjb3VudCBOdW1iZXI6ZGQCEQ8PFgIfAQUJVXNlcm5hbWU6ZGQCEw8PFgIfAQUJUGFzc3dvcmQ6ZGQCFQ8PF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Kl1bKzCQK1qbSRCwLFp6CQBwLCi9reAwKgt7D9CgKY5fiZAQLnxpq/AgKT2dzTDQK7hrWFCgKsuviBAQLnw+O9BAKywsaqBgL43fTxBQLonLasDzE6BdS22kepzqbhlPQS//T5e2ty";
    $post_data['txtID'] = 'idnumber';
    $post_data['txtUserName'] = 'username';
    $post_data['txtDupPassword'] = 'password';
    $post_data['Session_ID'] = '';
    $post_data['sessionid'] = '';
    $post_data['errorCode'] = '';
    $post_data['hdnSavedSearch'] = '';
    $post_data['hdnProdIds'] = '';
    $post_data['hdnMPSearchId'] = '';
    $post_data['hdnDisplaytype'] = '';
    $post_data['hdnMode'] = '';
    $post_data['hdnProductid'] = '';
    $post_data['hdnQuantity'] = '';
    $post_data['hdnBanner'] = '';
    $post_data['cos'] = '';
    $post_data = http_build_query($post_data);
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);

            $out = curl_exec($ch);
            echo $out;

问题是我收到服务器运行时错误作为响应。我真的不知道我应该如何形成网址。对此有何帮助?

提前致谢

1 个答案:

答案 0 :(得分:0)

修改 这是关于卷曲的另一篇文章:Trying to connect to ASPX site using CURL?

我不确定你是如何用curl做到这一点的,但是将证书发布到网站登录相对容易。请参阅以下关于此事的帖子:

Programmatically logging into a site

Using c#/ASP.NET to programmatically fake a login to a website

这个过程是这样的:

(1)向网页发出GET请求 (2)从你收到的回复中获取__VIEWSTATE (3)使用以下格式附加所需凭证的URL发布帖子:

yourtargetwebsiteaddress.com/targetloginpage?__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE={VS}&__EVENTVALIDATION={EV}&{UserNameControl}={UN}&{PasswordControl}={PW}

在哪里

  • {VS}是初始请求中的“查看”状态
  • {EV}是您初始请求中的事件验证
  • {UserNameControl}是用户名控件的ID,{UN}是您要发布的用户名
  • {PasswordControl}是网页上密码控件的ID,{PW}是您要发布的密码