简体   繁体   中英

How to correctly pass POST params to an asp.NET http handler from PHP?

I have an http handler (asp.NET 4.0) to process something:

public void ProcessRequest(HttpContext context)
{
    var request = context.Request;
    var userId = request["username"];
    var password = request["password"];
    var otherParam = request["otherparam"]
    ...
    // Process using userId, password and otherparam
    ...
}

I am trying to POST data to there by this PHP script:

function do_post_request($url, $params)
{
    $query = http_build_query ($params);    
    $contextData = array ( 
                    'method' => 'POST',
                    'header' => "Connection: close\r\n".
                                "Content-Length: ".strlen($query)."\r\n",
                    'content'=> $query );   
    $context = stream_context_create (array('http' => $contextData));   
    return  file_get_contents ($url, false, $context);
}

$url = "http://localhost:33614/dosomething";
$params  = array('username'=>'xyz', 'password'=>'123456', 'otherparam'=>'Sample from PHP');
$result = do_post_request($turl,$params);
var_dump($result);

The problem is, I am getting the username parameter in the http handler, but other two parameters are found null . I found those parameters as amp;password and amp;otherparam . I tried sending from python, c#, java etc but never found this problem.

How can I get the params? By the way, I don't have much knowledge of PHP.

amp; equals & after escaping for html, you got php escaping the characters or asp

Try to force it on php by doing:

http_build_query($params, '', '&');

instead of just http_build_query($params)

Looks like your POST body get's html-encoded, thus the original string username=xyz&password=123456&otherparam=Sample%20from%20PHP get's html-encoded to username=xyz&password=123456&otherparam=...

Your problem is that & get's encoded to & when sent to the server. You can do a little debug to see if the http_build_query() is the one that double encodes, or if stream_context_create() is responsible for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM