如何正确地将POST参数传递给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脚本将数据POST到那里:

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主体get的html已编码,因此原始字符串username=xyz&password=123456&otherparam=Sample%20from%20PHP get的html编码为username=xyz&password=123456&otherparam=...

您的问题是&在发送到服务器时被编码为&。您可以进行一些调试,看看http_build_query()是否是进行双重编码的那个,或者stream_context_create()是否对此负责。