2016-05-22 10:27

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.


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:


        [CONTENT_TYPE] => application/x-www-form-urlencoded
        [DOCUMENT_ROOT] => /path/account/
        [GATEWAY_INTERFACE] => CGI/1.1
        [HTTP_CONNECTION] => Keep-Alive
        [HTTP_HOST] =>
        [PATH] => /bin
        [PHPRC] => /etc/php5.3/cgi/
        [QUERY_STRING] => 
        [REDIRECT_STATUS] => 200
        [REMOTE_ADDR] =>
        [REMOTE_PORT] => 60149
        [REQUEST_URI] => /path/script.php
        [SCRIPT_FILENAME] => /path/account/directory/index/path/script.php
        [SCRIPT_NAME] => /path/script.php
        [SERVER_ADDR] =>
        [SERVER_ADMIN] =>
        [SERVER_NAME] =>
        [SERVER_PORT] => 80
        [SERVER_PROTOCOL] => HTTP/1.1
        [SERVER_SIGNATURE] => Apache/2.2.22 Server at 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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dsfs23434 dsfs23434 5年前

    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

    点赞 评论 复制链接分享