从响应了HTTPClient解压缩的GZip流 [英] Decompressing GZip Stream from HTTPClient Response

查看:1937
本文介绍了从响应了HTTPClient解压缩的GZip流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接到一个API,它返回的GZip编码的JSON,从WCF服务(WCF服务WCF服务)。我使用的了HTTPClient ,即可连接到API,并已经能够返回JSON对象为字符串。不过,我需要能够存储这个返回的数据在数据库中,因此我想最好的办法是恢复和JSON对象存储在沿着这些线路的数组或字节或东西。



什么我有麻烦特别是GZIP编码的解压缩,并一直在尝试许多不同的例子,但不能仍然得到它。



下面的代码是我如何建立我的连接,并得到响应,这是返回从API字符串的代码。




 公共字符串的getData(串富)
{
字符串的URL =;
HttpClient的客户端=新的HttpClient();
HttpResponseMessage响应;
串responseJsonContent;

{
client.DefaultRequestHeaders.Accept.Add(新MediaTypeWithQualityHeaderValue(应用/ JSON));
响应= client.GetAsync(URL +富)。结果;
responseJsonContent = response.Content.ReadAsStringAsync()结果。
返回responseJsonContent;
}
赶上(异常前)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
返回;
}
}




我一直以下象这些 StackExchange API 时,MSDN 和一对夫妇的计算器,但我一直没能获得任何这些为我工作。



什么是实现这一目标的最佳方式,我在连上正确的轨道?



谢谢你们


解决方案

只需实例HttpClient的是这样的:

  HttpClientHandler处理程序=新HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

使用(VAR的客户=新的HttpClient(处理))
{
//你的代码
}


I am trying to connect to an api, that returns GZip encoded JSON, from a WCF service (WCF service to WCF service). I am using the HTTPClient to connect to the API and have been able to return the JSON object as a string. However I need to be able to store this returned data in a database and as such I figured the best way would be to return and store the JSON object in an array or byte or something along those lines.

What I am having trouble with specifically is the decompressing of the GZip encoding and have been trying lots of different example but still cant get it.

The below code is how I am establishing my connection and getting a response, this is the code that returns a string from the API.

    public string getData(string foo)
    {
        string url = "";
        HttpClient client = new HttpClient();
        HttpResponseMessage response;
        string responseJsonContent;
        try
        {
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            response = client.GetAsync(url + foo).Result;
            responseJsonContent = response.Content.ReadAsStringAsync().Result;
            return responseJsonContent;
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(ex.Message);
            return "";
        }
    }

I have been following a few different examples like these StackExchange API, MSDN, and a couple on stackoverflow, but I haven't been able to get any of these to work for me.

What is the best way to accomplish this, am I even on the right track?

Thanks guys.

解决方案

Just instantiate HttpClient like this:

HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

using (var client = new HttpClient(handler))
{
    // your code
}

这篇关于从响应了HTTPClient解压缩的GZip流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆