繁体   English   中英

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

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

我有一个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
    ...
}

我正在尝试通过此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);

问题是,我在http处理程序中获取了username参数,但是发现其他两个参数为null 我发现这些参数为amp;passwordamp;otherparam 我尝试从python,c#,java等进行发送,但从未发现此问题。

我如何获得参数? 顺便说一句,我对PHP并不了解。

amp; 等于&转义html后,您得到php转义字符或asp

尝试通过以下方法在php上强制使用:

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

而不只是http_build_query($params)

看起来您的POST正文获得了html编码,因此原始字符串username=xyz&password=123456&otherparam=Sample%20from%20PHP得到了html编码至username=xyz&password=123456&otherparam=...

您的问题是&获取编码为& 发送到服务器时。 您可以进行一些调试,以查看http_build_query()是否是经过双重编码的代码,或者是否由stream_context_create()负责。

暂无
暂无

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

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