WebApi系列~对HttpClient的响应流进行解压
有时我们的请求头为ContentEncoding添加了gzip进行了压缩,而服务端返回数据时也会对它进行gzip压缩,如果在这种情况下,你直接头响应流会是乱码,而必须先进行压缩,大叔将这块的逻辑进行了抽取,它把抽取到了方法里,自动使用这个功能!
/// <summary>
/// 对流进行解压
/// </summary>
/// <param name="response"></param>
static void UnGZip(HttpResponseMessage response)
{
bool isGzip = response.Content.Headers.ContentEncoding.Contains("gzip");
if (isGzip)
{
Stream decompressedStream = new MemoryStream();
using (var gzipStream = new GZipStream(response.Content.ReadAsStreamAsync().Result, CompressionMode.Decompress))
{
gzipStream.CopyToAsync(decompressedStream);
}
decompressedStream.Seek(, SeekOrigin.Begin); var originContent = response.Content;
response.Content = new StreamContent(decompressedStream);
}
}
在GET,POST,PUT,DELETE方法的响应流时,进行装饰,把流进行解压即可!
public static T Post<T>(string url, object argument = null, CookieContainer cookieContainer = null)
{
string sret = "";
if (cookieContainer == null)
cookieContainer = new CookieContainer();
using (HttpClientHandler clientHandler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (HttpClient client = new HttpClient(clientHandler))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string sargument = Newtonsoft.Json.JsonConvert.SerializeObject(argument);
StringContent argumentContent = new StringContent(sargument, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(url, argumentContent).Result;
if (response.IsSuccessStatusCode)
{
UnGZip(response);
sret = response.Content.ReadAsStringAsync().Result;
}
else
{
throw new Exception(response.StatusCode.ToString());
}
if (!string.IsNullOrEmpty(sret))
{ T ret = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(sret);
return ret;
}
else
{
return default(T);
}
}
}
这样你的响应流就被解开了!
挺方便!
WebApi系列~对HttpClient的响应流进行解压的更多相关文章
- Http下的各种操作类.WebApi系列~通过HttpClient来调用Web Api接口
1.WebApi系列~通过HttpClient来调用Web Api接口 http://www.cnblogs.com/lori/p/4045413.html HttpClient使用详解(java版本 ...
- windows phone使用sharpcompress进行解压压缩文件
在做移动端时,当我们需要从服务器获得多个文件时,为了节约流量,服务器一般会返回一个压缩包,那我们就是下载完成后,在手机中进行解压到指定位置 SharpCompress就是可以在手机中进行解压一个类库( ...
- 爱上MVC系列~过滤器实现对响应流的处理
回到目录 MVC的过滤器相信大家都用过,一般用来作权限控制,因为它可以监视你的Action从进入到最后View的渲染,整个过程ActionFilter这个过滤器都参与了,而这给我们的开发带来了更多的好 ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递
回到目录 上一讲中介绍了使用HttpClient如何去调用一个标准的Web Api接口,并且我们知道了Post,Put方法只能有一个FromBody参数,再有多个参数时,上讲提到,需要将它封装成一个对 ...
- WebApi系列~通过HttpClient来调用Web Api接口~续~实体参数的传递 【转】
原文:http://www.cnblogs.com/lori/p/4045633.html 下面定义一个复杂类型对象 public class User_Info { public int Id { ...
- WebApi系列~通过HttpClient来调用Web Api接口
回到目录 HttpClient是一个被封装好的类,主要用于Http的通讯,它在.net,java,oc中都有被实现,当然,我只会.net,所以,只讲.net中的HttpClient去调用Web Api ...
- 对zip文件进行解压操作和对一个文件进行压缩操作
注意这里用的是apche下的zip package org.springframework.validation; import org.apache.tools.zip.ZipEntry; impo ...
- .Net Core HttpClient处理响应压缩
前言 在上篇文章[ASP.NET Core中的响应压缩]中我们谈到了在ASP.NET Core服务端处理关于响应压缩的请求,服务端的主要工作就是根据Content-Encoding头信息判断采 ...
- WebApi系列~目录
回到占占推荐博客索引 写了这个系列的文章不少了,也应该为大家写个目录了,最近很刮了很多SOA的风,很多企业都将自己的系统进行分割,通常是按模块进行拆分,为这个模块提供统一的接口提供业务服务,这不紧可以 ...
随机推荐
- C#代码总结04---通过创建临时表DataTable进行临时编辑删除
<script type="text/javascript"> //删除 function Delete(hdGuid) { $("#hdGuid" ...
- APM(pixhawk)飞控疑难杂症解决方法汇总(持续更新)
本文转自下面博主 https://blog.csdn.net/junzixing/article/details/79310159 APM/Pixhawk常用链接汇总(持续更新) https://bl ...
- 学习使用Mendeley1
原文来自:https://www.mendeley.com/guides/desktop/01-desktop-interface 1.添加文件菜单 - 使用此功能将新条目添加到您的Mendeley库 ...
- 字符串转义为HTML
有时候后台返回的数据中有字符串,并需要将字符串转化为HTML,下面封装了一个方法,如下 // html转义 function htmlspecialchars_decode(string, quote ...
- [LeetCode] Keys and Rooms 钥匙与房间
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, an ...
- HTML标签 按功能排序
按功能类别排列 New : HTML5 中的新标签. 基础 标签 描述 <!DOCTYPE> 定义文档类型. <html> 定义 HTML 文档. <title> ...
- LeetCode编程训练 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- React Native 填坑之神奇的报错,已解决
下面对报错进行一下详细描述: 在debug时,点着点着,就会发生: 1.手机显示如下 : Attempted to transition from state `RESPONDER_INACTIVE_ ...
- Spring AOP实现 Bean字段合法性校验
使用SpringAop 验证方法参数是否合法 先定义两个注解类ValidateGroup 和 ValidateFiled ValidateGroup .java package com.zf.an ...
- [Swift]LeetCode100. 相同的树 | Same Tree
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...