2014-10-27 55 views
0

我們有一個在asp.net中編程的搜索引擎。 因爲我可以預測和捲曲或多或少有直接的聯繫,所以我設法解決個別結果。 但我無法控制結果列表,下面是它的工作原理:cURL和ASP.NET:發佈參數問題

在搜索頁面上,我們必須通過複選框菜單選擇要搜索的數據庫。 一旦我檢查了我想要搜索的分貝,我點擊「搜索」按鈕,它會將我轉到搜索頁面,並將所選數據庫考慮在內。

如果我嘗試使用直接鏈接進入搜索頁面,它不起作用,因爲它不知道在哪個數據庫中進行搜索。 我想看看在後的參數與螢火蟲,我得到了以下幾點:

Checkbox_db1 on 
__EVENTARGUMENT 
__EVENTTARGET LinkButtonCategory 
__VIEWSTATE zeyhbf5vg41g6a4f1ezragf136er46ga4gfv658a4r6g4 (something looking like that but longer) 

這是我嘗試在捲曲:

$ch = curl_init(); 
$fields = array ('Checkbox_db1' => 'on', '__EVENTARGUMENT' => '', 
       '__EVENTTARGET' => 'LinkButtonCategory', '__VIEWSTATE' => ''); 
$postvars = ''; 
foreach($fields as $key=>$value) 
{ 
    $postvars .= $key.'='.$value.'&'; 
} 
rtrim ($postvars, '&'); 

curl_setopt ($ch, CURLOPT_URL, "monsite.com/choosedb.aspx"); 
curl_setopt ($ch, CURLOPT_POST, count($fields)); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postvars); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 

$output1 = curl_exec($ch); 

$fields2 = array ('TxtBox1' => 'value1', 'Txtbox2' => 'value2', '__EVENTARGUMENT' => '', 
       '__EVENTTARGET' => '', '__VIEWSTATE' => ''); 
$postvars = ''; 
foreach($fields2 as $key=>$value) 
{ 
    $postvars .= $key.'='.$value.'&'; 
} 
rtrim ($postvars, '&'); 

curl_setopt ($ch, CURLOPT_URL, "monsite.com/search.aspx"); 
curl_setopt ($ch, CURLOPT_POST, count($fields2)); 
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postvars); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true); 

$output2 = curl_exec($ch); 

但是,當然,這不工作... 。問題是,我對ASP.NET不熟悉:/ 任何人都可以提供幫助嗎?在此先感謝

+0

這需要時間,但我發現如何提取VIEWSTATE崗位。 – 2014-10-28 10:30:55

回答

0

所以,首先你得到一個普通的捲曲獲得的初始頁面。

然後你必須提取VIEWSTATE參數:

$regexViewstate = '/__VIEWSTATE\" value=\"(.*)\"/i'; 

function regexExtract($text, $regex, $regs, $nthValue) 
{ 
if (preg_match($regex, $text, $regs)) { 
$result = $regs[$nthValue]; 
} 
else { 
$result = ""; 
} 
return $result; 
} 

$viewstate = regexExtract($data,$regexViewstate,$regs,1); 

你讓你的新職位:

$postData = '__EVENTARGUMENT=&__EVENTTARGET=LinkButtonCategory&__VIEWSTATE='; 
$postData .= rawurlencode($viewstate).'&TxtBox1=value1&TxtBox2=value2'; 

curl_setOpt($ch, CURLOPT_POST, TRUE); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_URL, $urlLogin); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);  

$output = curl_exec($ch);