HttpClient POST generates GET instead

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?


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

链接地址: http://www.djcxy.com/p/48806.html

上一篇: JQuery,Spring MVC @RequestBody和JSON

下一篇: HttpClient POST生成GET