簡體   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