2011-02-07 66 views
0

我需要登錄到某個網站才能嘗試數據下載。我正在嘗試通過PHP腳本來做到這一點。通過PHP(Cookie/POST值問題?)登錄到ASP應用程序

登錄頁面位於:

http://abc.example.com/login.aspx 

其中有這樣的形式(除去多餘的元素):

<form name="Form1" method="post" action="login.aspx" id="Form1"> 
<input type="hidden" name="__EVENTTARGET" value="" /> 
<input type="hidden" name="__EVENTARGUMENT" value="" /> 
<input type="hidden" name="__VIEWSTATE" value="dDw1MTU3NTkxNTI7O2w8Y2hrYm94Oz4+08TlRVm+gb75yz3dIctChP3qf/E=" /> 

<script language="javascript" type="text/javascript"> 
<!-- 
    function __POSTBack(eventTarget, eventArgument) { 
     var thisform; 
     if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) { 
      thisform = document.Form1; 
     } 
     else { 
      thisform = document.forms["Form1"]; 
     } 
     thisform.eventTarget.value = eventTarget.split("$").join(":"); 
     thisform.eventArgument.value = eventArgument; 
     thisform.submit(); 
    } 
// --> 
</script> 
<input name="text_userid" id="text_userid" type="text" /> 
<input name="text_password" type="password" maxlength="30" id="text_password" /> 
<input name="chkbox" id="chkbox" type="checkbox" value="checkbox" /> 
<a id="submit" href="javascript:__POSTBack('submit','')"><img src="images/login.jpg"></a> 
</form> 

我試圖通過PHP登錄到它,但我我不知道我做錯了什麼。下面是我在做什麼:

<?php 
$pages = array(
      'login_pre' => 'http://abc.example.com/login.aspx', 
      'login' => 'http://abc.example.com/login.aspx'); 
$ch = curl_init(); 

//Set options for curl session 
$options = array(CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6', 
     CURLOPT_HEADER => TRUE, 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_COOKIEFILE => 'cookie.txt', 
     CURLOPT_COOKIEJAR => 'cookies.txt'); 

$options[CURLOPT_URL] = $pages['login_pre']; 
curl_setopt_array($ch, $options); 
$login_pre_content = curl_exec($ch); 

preg_match('/__VIEWSTATE" value="(.*)"/', $login_pre_content, $matches); 
$VIEWSTATE = $matches[1]; 

//Login 
$options[CURLOPT_URL] = $pages['login']; 
$options[CURLOPT_POST] = TRUE; 

$options[CURLOPT_POSTFIELDS] = '__EVENTTARGET=submit&__EVENTARGUMENT=&__VIEWSTATE='.$VIEWSTATE.'&text_userid=MYEMAIL&text_password=MYPASSWORD&chkbox=on'; 

$options[CURLOPT_FOLLOWLOCATION] = TRUE; 
curl_setopt_array($ch, $options); 
$login_post_content = curl_exec($ch); 

echo $login_post_content; 

//Close curl session 
curl_close($ch); 
?> 

如果我上面,我收到以下錯誤信息(由$ login_post_content生成的網頁內):

Invalid length for a Base-64 char array. 

堆棧跟蹤如下:

[FormatException: Invalid length for a Base-64 char array.] 
    System.Convert.FromBase64String(String s) +0 
    System.Web.UI.LosFormatter.Deserialize(String input) +24 
    System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101 

[HttpException (0x80004005): Invalid_Viewstate 
    Client IP: MYIPADDRESS 
    Port: 24885 
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 
    ViewState: dDw1MTU3NTkxNTI7O2w8Y2hrYm94Oz4 08TlRVm gb75yz3dIctChP3qf/E= 
    Http-Referer: 
    Path: /login.aspx.] 
    System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +442 
    System.Web.UI.Page.LoadPageViewState() +18 
    System.Web.UI.Page.ProcessRequestMain() +441 

HOEVER,如果我修改代碼如下:

$options[CURLOPT_POSTFIELDS] = '...&__VIEWSTATE='.base64_encode($VIEWSTATE).'...'); 

我收到以下錯誤信息:

Unable to validate data. 

堆棧跟蹤如下:

[HttpException (0x80004005): Unable to validate data.] 
    System.Web.Configuration.MachineKey.GetDecodedData(Byte[] buf, Byte[] modifier, Int32 start, Int32 length, Int32& dataLength) +195 
    System.Web.UI.LosFormatter.Deserialize(String input) +59 

[HttpException (0x80004005): Authentication of viewstate failed. 1) If this is a cluster, edit <machineKey> configuration so all servers use the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster. 2) Viewstate can only be posted back to the same page. 3) The viewstate for this page might be corrupted.] 
    System.Web.UI.LosFormatter.Deserialize(String input) +117 
    System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +101 

[HttpException (0x80004005): Invalid_Viewstate 
    Client IP: MYIPADDRESS 
    Port: 25029 
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 
    ViewState: ZER3MU1UVTNOVGt4TlRJN08ydzhZMmhyWW05NE96NCswOFRsUlZtK2diNzV5ejNkSWN0Q2hQM3FmL0U9 
    Http-Referer: 
    Path: /login.aspx.] 
    System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +442 
    System.Web.UI.Page.LoadPageViewState() +18 
    System.Web.UI.Page.ProcessRequestMain() +441 

回答

0

表單字段的值需要進行url編碼。具體來說,ViewState的值需要適當的urlencoded(它有+和=號,需要轉義)。