Note that there are some explanatory texts on larger screens.

plurals
  1. POcURL post data to asp.net page
    primarykey
    data
    text
    <p>I am trying to call the <code>__doPostback</code> javascript function in a asp.net page from php using curl.</p> <p>I learnt that this can be done by making a post request to the asp.net page with the appropriate parameters.</p> <p>So in curl, </p> <ol> <li>I make a get request / just use <code>file_get_contents</code> to retrieve the initial page.</li> <li>From this, I extract the values for <code>__VIEWSTATE</code> and <code>__EVENTVALIDATION</code>.</li> </ol> <p>So far everything seems ok.</p> <p>Now, I understand that we need to make a post request using cURL with <code>__VIEWSTATE</code> and other parameters required. ( values for the fields present in the asp.net form )</p> <p>I am unable to construct the <code>CURLOPT_POSTFIELDS</code> correctly.</p> <p>For instance, I am trying this out,</p> <pre><code>$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&amp;__EVENTARGUMENT='.('$2'); $postoptions2 = '&amp;__VIEWSTATE='.urlencode($viewState) ; $otherparams = '&amp;ctl00$ContentPlaceHolder1$ddlName=Abc'; </code></pre> <p>And before using setopt for <code>CURLOPT_POSTFIELDS</code>, I am doing,</p> <pre><code>urlencode ($postoptions1.$postoptions2.$otherparams) </code></pre> <p>This does not work. The submit results are not shown, which means, the required parameter <code>__VIEWSTATE</code> was not found in my post request.</p> <p>If I change the order of the parameters and place <code>__VIEWSTATE</code> as the first parameter, the results page is shown but the other parameter values are not honoured. </p> <p>I think there is some problem with the way I am encoding the parameters.</p> <p>Please tell me how to construct the parameters for the post request to a asp.net page.</p> <p>Thanks.</p> <p>--Edited--</p> <p>Here is the complete code: $resultsPerPage='10'; $url = "www.example.com"; // url changed </p> <p>$curl_connection = curl_init($url); function sendCurl($curl_connection,$url,$params,$isPost=false) {</p> <pre><code>//$post_string = $params; $post_string = http_build_query($params); //$post_string = build_query_string($params); //$post_string = urlencode($params); echo 'After Encode'.$post_string; $cookie="/cookie.txt"; //set options curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 300); curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_connection, CURLOPT_HEADER, 0); // don't return headers curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl_connection,CURLOPT_REFERER, $url); if($isPost) { curl_setopt ($curl_connection, CURLOPT_POST, true); //set data to be posted curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); curl_setopt($curl_connection,CURLOPT_COOKIEJAR,$cookie); } else { curl_setopt($curl_connection,CURLOPT_COOKIEFILE,$cookie); } $response1 = curl_exec($curl_connection); if($response1 === false) { echo 'Curl error: ' . curl_error($curl_connection); } else { echo 'Operation completed without any errors'; } return $response1; } **// First time, get request to asp.net page </code></pre> <p><code>$response1 = sendCurl($curl_connection,$url,'',false);<br> $viewState=getVStateContent($response1);<br> $eventValidation =getEventValidationContent($response1);<br> $simpleParams = '&amp;__VIEWSTATE='.$viewState.'&amp;ctl00$ContentPlaceHolder1$ddlManuf=&amp;ctl00$ContentPlaceHolder1$ddlCrossType=&amp;ctl00$ContentPlaceHolder1$ddlPageSize='.$resultsPerPage.'&amp;ctl00$ContentPlaceHolder1$btnSearch=Search&amp;ctl00_ToolkitScriptManager1_HiddenField=&amp;__EVENTTARGET=&amp;__EVENTARGUMENT=';<br> // Second post - for submitting the search form<br> $response2= sendCurl($curl_connection,$url,$simpleParams,true);</code> ----**</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload