0

I'm developing a twitter third party application. while looking for a way to send a web request, I found two classes: Windows.Web.Http.HttpClient and System.Net.Http.HttpClient.

Both classes do not seem to have much difference, but they get very different esults in the same request.

When I send requests with Windows.Web.Http.HttpClient, it works well.

public async Task<string> Request(Method method, string url, string postData)
{
    var http = new Windows.Web.Http.HttpClient();
    Windows.Web.Http.HttpResponseMessage response;

    if (method == Method.POST)
    {

        var httpContent = new Windows.Web.Http.HttpStringContent(postData, Windows.Storage.Streams.UnicodeEncoding.Utf8);
        httpContent.Headers.ContentType = Windows.Web.Http.Headers.HttpMediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
        response = await http.PostAsync(new Uri(url), httpContent);
    }
    else
    {
        response = await http.GetAsync(new Uri(url));
    }

    return await response.Content.ReadAsStringAsync();
}

But If I send requests with System.Net.Http.HttpClient. I receive wrong response.

(But, when I access the request url with a web browser, it works well not like image below)

public async Task<string> Request(Method method, string url, string postData)
{
    var http = new System.Net.Http.HttpClient();
    System.Net.Http.HttpResponseMessage response;

    if (method == Method.POST)
    {

        var httpContent = new System.Net.Http.StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
        response = await http.PostAsync(new Uri(url), httpContent);
    }else
    {
        response = await http.GetAsync(new Uri(url));
    }


    return await response.Content.ReadAsStringAsync();
}

Why is this different? and how can I solve this problem?

blurfx
  • 1,270
  • 11
  • 22
  • 1
    Response on screenshot for System.Net.Http.HttpClient looks like it might be compressed (gzip) but HttpClient didn't decompressed it. You may check headers of both, request and response, whether HTTP headers such as Accept-Encoding or Content-Encoding are sent/received properly. You can also check whether System.Net.Http.HttpClient implements HTTP2 properly, because support for it [has been added recently into .NET Framework 4.6.2](http://stackoverflow.com/a/38462037/1286670) – Ňuf Feb 16 '17 at 12:06
  • @Nuf you're right ! `HttpClient` didn't decompress response data. so I wrote code a little bit more. – blurfx Feb 16 '17 at 12:32

1 Answers1

0

The problem was HttpClient didn't decompress gzip data as Nuf said in the comments.

so I just wrote tiny code for gzip decompression.

public async Task<string> Request(Method method, string url, string postData)
{
    var handler = new System.Net.Http.HttpClientHandler()
    {
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
    };
    var http = new System.Net.Http.HttpClient(handler);
    System.Net.Http.HttpResponseMessage response;
    if (method == Method.POST)
    {

        var httpContent = new System.Net.Http.StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
        response = await http.PostAsync(new Uri(url), httpContent);
    }
    else
    {
        response = await http.GetAsync(new Uri(url));
    }


    return await response.Content.ReadAsStringAsync();
}
blurfx
  • 1,270
  • 11
  • 22