简体   繁体   English

Windows.Web.Http.HttpClient和System.Net.Http.HttpClient收到不同的响应

[英]Windows.Web.Http.HttpClient and System.Net.Http.HttpClient receive different responses

I'm developing a twitter third party application. 我正在开发一个Twitter第三方应用程序。 while looking for a way to send a web request, I found two classes: Windows.Web.Http.HttpClient and System.Net.Http.HttpClient . 在寻找发送Web请求的方法时,我发现了两个类: Windows.Web.Http.HttpClientSystem.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. 当我使用Windows.Web.Http.HttpClient发送请求时,它运行良好。

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 . 但是如果我使用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? 我该如何解决这个问题?

The problem was HttpClient didn't decompress gzip data as Nuf said in the comments. 问题是HttpClient没有像Nuf在评论中所说的那样解压缩gzip数据。

so I just wrote tiny code for gzip decompression. 所以我只是为gzip解压缩编写了一些小代码。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM