简体   繁体   English

如何正确地将POST参数从PHP传递到asp.NET http处理程序?

[英]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: 我有一个http处理程序(asp.NET 4.0)处理某些内容:

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: 我正在尝试通过此PHP脚本将数据发布到那里:

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 . 问题是,我在http处理程序中获取了username参数,但是发现其他两个参数为null I found those parameters as amp;password and amp;otherparam . 我发现这些参数为amp;passwordamp;otherparam I tried sending from python, c#, java etc but never found this problem. 我尝试从python,c#,java等进行发送,但从未发现此问题。

How can I get the params? 我如何获得参数? By the way, I don't have much knowledge of PHP. 顺便说一句,我对PHP并不了解。

amp; equals & after escaping for html, you got php escaping the characters or asp 等于&转义html后,您得到php转义字符或asp

Try to force it on php by doing: 尝试通过以下方法在php上强制使用:

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

instead of just http_build_query($params) 而不只是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=... 看起来您的POST正文获得了html编码,因此原始字符串username=xyz&password=123456&otherparam=Sample%20from%20PHP得到了html编码至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. 您可以进行一些调试,以查看http_build_query()是否是经过双重编码的代码,或者是否由stream_context_create()负责。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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