本文介绍了卷曲发布的数据asp.net页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话给 __ doPostback 中的JavaScript从PHP使用curl一个asp.net页面的功能。

我了解到,这可以通过一个POST请求asp.net页面适当的参数来实现。

因此​​,在袅袅,


  1. 我做一个GET请求/只使用的file_get_contents 来获取初始页面。

  2. 从这里,我提取 __ VIEWSTATE __ EVENTVALIDATION 的值。

到目前为止,一切看起来正常。

现在,我明白,我们需要使用cURL POST请求与 __ VIEWSTATE 和所需的其它参数。 (对于字段的值present在asp.net形式)

我无法构建 CURLOPT_POSTFIELDS 正常。

比如说,我想这一点,

<$p$p><$c$c>$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&__EVENTARGUMENT='.('$2');
$ postoptions2 ='和; __ VIEWSTATE ='urlen code($视图状态)。
$ otherparams ='和; ctl00 $ ContentPlaceHolder1 $ ddlName = ABC';

和使用SETOPT为 CURLOPT_POSTFIELDS 之前,我做的,

  urlen code($ postoptions1。$ postoptions2。$ otherparams)

这是行不通的。提交结果没有显示,这意味着,所需的参数 __ VIEWSTATE 在我的岗位要求没有被发现。

如果我更改参数和地点的顺序 __ VIEWSTATE 作为第一个参数,结果页面显示,但其他参数值不兑现。

我觉得有一些问题,我的编码参数的方法。

请告诉我如何构造参数的POST请求一个asp.net页面。

感谢。

- 编辑 -

下面是完整的code:
$ resultsPerPage = '10';
$ URL =www.example.com; // URL改变

$ curl_connection = curl_init($网址);
    功能sendCurl($ curl_connection,$网址,$参数,可以$ isPost = FALSE){

  // $ post_string = $ PARAMS;
$ post_string = http_build_query($ params)方法;
// $ post_string = build_query_string($ params)方法;
// $ post_string = urlen code($ params)方法;回声$ post_string恩code后'。
   $饼干=/ cookie.txt;
   //设置选项
    curl_setopt($ curl_connection,CURLOPT_CONNECTTIMEOUT,300);
    curl_setopt($ curl_connection,CURLOPT_USERAGENT,
      Mozilla的/ 4.0(兼容; MSIE 6.0; Windows NT的5.1));
    curl_setopt($ curl_connection,CURLOPT_RETURNTRANSFER,真正的);
    curl_setopt($ curl_connection,CURLOPT_HEADER,0); //不返回头    curl_setopt($ curl_connection,CURLOPT_SSL_VERIFYPEER,FALSE);
    curl_setopt($ curl_connection,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ curl_connection,CURLOPT_REFERER,$网址);
    如果($ isPost){
        curl_setopt($ curl_connection,CURLOPT_POST,真正的);
        //要发布的一组数据
        curl_setopt($ curl_connection,CURLOPT_POSTFIELDS,$ post_string);
        curl_setopt($ curl_connection,CURLOPT_COOKIEJAR,$饼干);    }
    其他{
    curl_setopt($ curl_connection,CURLOPT_COOKIEFILE,$饼干);
    }
   $ response1 = curl_exec($ curl_connection);
   如果($ response1 ===假)
    {
        呼应卷曲的错误:。 curl_error($ curl_connection);
    }
    其他
{
    回声操作完成,没有任何错误;
}
   返回$ response1;
} // **第一次获得请求asp.net页面

$ response1 = sendCurl($ curl_connection,$网址,'',FALSE);结果
$视图状态= getVStateContent($ response1);结果
$ eventValidation = getEventValidationContent($ response1);结果
$ simpleParams = '&__VIEWSTATE='.$viewState.'&ctl00$ContentPlaceHolder1$ddlManuf=&ctl00$ContentPlaceHolder1$ddlCrossType=&ctl00$ContentPlaceHolder1$ddlPageSize='.$resultsPerPage.'&ctl00$ContentPlaceHolder1$btnSearch=Search&ctl00_ToolkitScriptManager1_HiddenField=&__EVENTTARGET=&__EVENTARGUMENT=';

//第二个职位 - 提交搜索表单结果
$响应2 = sendCurl($ curl_connection,$网址,$ simpleParams,真实);

---- **


解决方案

你想要的是的,这将格式化数组作为适当的HTTP参数。

修改的:要澄清这或许应该是这​​样的:

  $ PARAMS =阵列(
    __EVENTTARGET'=&GT; ctl00 $ ContentPlaceHolder1 $格列夫',
    __EVENTARGUMENT'=&GT; '$ 2',
    __VIEWSTATE'=&GT; $视图状态,
    ctl00 $ ContentPlaceHolder1 $ ddlName'=&GT; 'ABC'
);curl_setopt($ curlHandler,CURLOPT_POSTFIELDS,http_build_query($ params)方法);

此外,什么是 ctl00 $ ContentPlaceHolder1 $ ddlName 应该是什么?

I am trying to call the __doPostback javascript function in a asp.net page from php using curl.

I learnt that this can be done by making a post request to the asp.net page with the appropriate parameters.

So in curl,

  1. I make a get request / just use file_get_contents to retrieve the initial page.
  2. From this, I extract the values for __VIEWSTATE and __EVENTVALIDATION.

So far everything seems ok.

Now, I understand that we need to make a post request using cURL with __VIEWSTATE and other parameters required. ( values for the fields present in the asp.net form )

I am unable to construct the CURLOPT_POSTFIELDS correctly.

For instance, I am trying this out,

$postoptions1='__EVENTTARGET='.('ctl00$ContentPlaceHolder1$gRef').'&__EVENTARGUMENT='.('$2');
$postoptions2 =  '&__VIEWSTATE='.urlencode($viewState) ;
$otherparams = '&ctl00$ContentPlaceHolder1$ddlName=Abc';

And before using setopt for CURLOPT_POSTFIELDS, I am doing,

urlencode ($postoptions1.$postoptions2.$otherparams)

This does not work. The submit results are not shown, which means, the required parameter __VIEWSTATE was not found in my post request.

If I change the order of the parameters and place __VIEWSTATE as the first parameter, the results page is shown but the other parameter values are not honoured.

I think there is some problem with the way I am encoding the parameters.

Please tell me how to construct the parameters for the post request to a asp.net page.

Thanks.

--Edited--

Here is the complete code:$resultsPerPage='10';$url = "www.example.com"; // url changed

$curl_connection = curl_init($url); function sendCurl($curl_connection,$url,$params,$isPost=false) {

//$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

$response1 = sendCurl($curl_connection,$url,'',false);
$viewState=getVStateContent($response1);
$eventValidation =getEventValidationContent($response1);
$simpleParams = '&__VIEWSTATE='.$viewState.'&ctl00$ContentPlaceHolder1$ddlManuf=&ctl00$ContentPlaceHolder1$ddlCrossType=&ctl00$ContentPlaceHolder1$ddlPageSize='.$resultsPerPage.'&ctl00$ContentPlaceHolder1$btnSearch=Search&ctl00_ToolkitScriptManager1_HiddenField=&__EVENTTARGET=&__EVENTARGUMENT=';
// Second post - for submitting the search form
$response2= sendCurl($curl_connection,$url,$simpleParams,true);
----**

解决方案

What you want is http_build_query, which will format an array as proper HTTP parameters.

Edit: To clarify what this should probably look like:

$params = array(
    '__EVENTTARGET' => 'ctl00$ContentPlaceHolder1$gRef',
    '__EVENTARGUMENT' => '$2',
    '__VIEWSTATE' => $viewState,
    'ctl00$ContentPlaceHolder1$ddlName' => 'Abc'
);

curl_setopt($curlHandler, CURLOPT_POSTFIELDS, http_build_query($params));

Also, what's ctl00$ContentPlaceHolder1$ddlName supposed to be?

这篇关于卷曲发布的数据asp.net页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:15