doubanduo7620 2016-05-22 10:27
浏览 59
已采纳

HttpClient POST会生成GET

Lack of sleep and hours of staring at my code made me give in.

Here is my problem:

I want to send a POST request within my c# Forms app, and retrieve the result.

Everything works, except for transmitting the POST body: Instead of my contents, the request turns up empty and, as I found out only a longtime later, with a GET REQUEST_METHOD instead.

Server side should not be the problem, when I send a request by web form (HTML), everything works fine.

c#:

async Task<string> reqres(string name, string logs)
{
    using (var client = new HttpClient())
    {
        string handler = settings.handler;  // php script on server

        var values = new Dictionary<string, string>();
        values.Add("name", name);
        values.Add("logs", logs);

        var response = await client.PostAsync(hand, new FormUrlEncodedContent(values));
        var contents = response.Content.ReadAsStringAsync().Result;
        return contents;
    }
}

I do always get a response from my server, but the server always says it was given a GET request.

Here is my server output:

PHP:

Array
    (
        [CONTENT_TYPE] => application/x-www-form-urlencoded
        [DOCUMENT_ROOT] => /path/account/example.com/index
        [GATEWAY_INTERFACE] => CGI/1.1
        [HTTP_CONNECTION] => Keep-Alive
        [HTTP_HOST] => www.example.com
        [PATH] => /bin
        [PHPRC] => /etc/php5.3/cgi/example.com
        [QUERY_STRING] => 
        [REDIRECT_STATUS] => 200
        [REMOTE_ADDR] => xx.xxx.xx.xxx
        [REMOTE_PORT] => 60149
        [REQUEST_METHOD] => GET
        [REQUEST_URI] => /path/script.php
        [SCRIPT_FILENAME] => /path/account/directory/index/path/script.php
        [SCRIPT_NAME] => /path/script.php
        [SERVER_ADDR] => xxx.x.xxx.xxx
        [SERVER_ADMIN] => webmaster@example.com
        [SERVER_NAME] => www.example.com
        [SERVER_PORT] => 80
        [SERVER_PROTOCOL] => HTTP/1.1
        [SERVER_SIGNATURE] => Apache/2.2.22 Server at www.example.com Port 80

    [SERVER_SOFTWARE] => Apache/2.2.22
    [PHP_SELF] => /path/script.php
    [REQUEST_TIME] => 1463912396
)
</pre><br />
Nope.   // result of $_POST query
Array   // print_r($_POST);
(
)

Who can tell me what's wrong? What strikes your eye? What could be the source of this problem?

  • 写回答

1条回答 默认 最新

  • dsfs23434 2016-05-22 10:39
    关注

    AAAAAAAARGH! Curses!

    It WAS a redirect after all...

    I let the 200 (OK) trick me into looking elsewhere... JonStirling kindly made me aware that there still might be redirects happening before that.

    And, sure enough, after I made sure I wasn't redirected for !www. -> www., it worked as a POST.

    Ugh... I really think the layers are poorly implemented in this case

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么