模拟请求(模拟header gzip解压 泛型)
WebClient
HeaderData是自定义类对象,存储header信息
private static T GetDataCommonMethod<T>(string url, string host, HeaderData headerData) where T : class
{
//注意Host和请求基网址各个请求可能不同
var client = new WebClient();
var headerStr = $"Host: {host}" + "\n" +
"User-Agent: ......" + "\n" +
@"Accept: application/json, text/plain, */*" + "\n" +
"Accept-Encoding: gzip, deflate" + "\n" +
"Accept-Language: zh-cn" + "\n" +
$"Cookie: m={headerData.m}; u={headerData.u}; wx={headerData.wx}; ......" + "\n";
var rawHeaders = headerStr.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries).ToList();
var headerPairs = rawHeaders.Select(x =>
{
var items = x.Split(new[] { ':' }, 2, StringSplitOptions.RemoveEmptyEntries);
var key = items[0].Trim();
var value = items[1].Trim();
//ValueTuple是C# 7中的语法,.net framework框架在4.7以上自带
return new ValueTuple<string, string>(key, value);
}).ToList();
headerPairs.ForEach(x =>
{
client.Headers.Set(x.Item1, x.Item2);
});
//先gzip解压再转string
byte[] rawBytes = client.DownloadData(url);
var stream = new MemoryStream(rawBytes);
GZipStream g = new GZipStream(stream, CompressionMode.Decompress);
//gzip最后四位是原始长度
var length = BitConverter.ToInt32(rawBytes, rawBytes.Length - 4);
byte[] bytes = new byte[length];
g.Read(bytes, 0, bytes.Length);
//编码是UTF8
string s0 = System.Text.Encoding.UTF8.GetString(bytes); T result = JsonConvert.DeserializeObject<T>(s0);
return result;
}
HttpClient
var baseAddress = new Uri("");
var yourText = "";
//UseCookies:处理程序是否使用 CookieContainer 属性来存储服务器Cookie 并在发送请求时使用这些 Cookie
using (var handler = new HttpClientHandler { UseCookies = false })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
var formContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("id", "1")
});
//此处确定请求方式(POST)
var message = new HttpRequestMessage(HttpMethod.Post, "");
//添加Cookie信息
message.Headers.Add("Cookie", yourText);
message.Content = formContent;
//使用SendAsync方法,把请求传进去HttpRequestMessage
var result = await client.SendAsync(message);
strContent = await result.Content.ReadAsStringAsync();
var retObj = JsonConvert.DeserializeObject<ResultDto>(strContent);
}
注意事项
1.不能带
Connection: keep-alive
报错:InnerException = {"Keep-Alive 和 Close 不能使用此属性设置。\r\n参数名: value"}
2."User-Agent","Accept-Language","application/json"这些,注意中间不要有空格
报错:指定的值含有无效的 HTTP 标头字符。\r\n参数名: name
模拟请求(模拟header gzip解压 泛型)的更多相关文章
- Android获取网络数据进行GZIP解压
说明:现在很多网站都会在回传数据的时候进行GZIP压缩,我们可以在请求头中申明支持GZIP压缩.可以减轻网络传输压力,Xutils中已经实现. 下面是一个DEMO,便于理解. private void ...
- httplib 和 httplib2区别之 gzip解压
HTTP请求头Accept-encoding: gzip信息告诉服务器,如果它有任何新数据要发送给时,请以压缩的格式发送.如果服务器支持压缩,它将返回由 gzip 压缩的数据并且使用Content-e ...
- VB6进行GZIP解压&C#进行GZIP压缩和解压
VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...
- iOS gzip解压
1. 导入libz库(如:libz 1.2.5.dylib) 2. 引入头文件 #import "zlib.h" 3. 实现解压(输入输出都为NSData对象) -(NSData ...
- asp.net实现GZip压缩和GZip解压
最近在开发一个网站doc.115sou.com,使用到了GZip压缩技术,经过多次搜索找到asp.net中用GZip对数据压缩和解压缩非常方便,当我第一次拿到这个类的时候却感觉很迷茫,无从下手.主要是 ...
- gzip解压和压缩
我发现网上很少有这样完整例子,加上英文有不好,走了好多弯路.我现在把从网上找到例子帖出来,可以解压HTTP gzip的 #include <stdlib.h> #include <s ...
- gzip解压压缩的字符串数据
import urllib2 from StringIO import StringIO import gzip def loadData(url): request = urllib2.Reques ...
- gzip解压文件报错
#tar -xvf jdk-8u131-linux-x64.tar.gz,执行命令后报错如下: gzip: stdin: not in gzip format tar: Child returned ...
- 5.post上传和压缩、插件模拟请求
gzip gzip一种压缩方式,或者是文件形式,它主要用于网络传输数据的压缩 gzip压缩好不好用 浏览器:网速一定.内容越小.请求响应的速度是不是更快 手机server:返回数据类型是json/ ...
随机推荐
- 设计模式-适配器模式(Go语言描写叙述)
在上一篇博客设计模式-策略模式(Go语言描写叙述)中我们用最简单的代码用go语言描写叙述了设计模式中的策略模式,用最简单的实例来描写叙述相信能够让刚開始学习的人能够非常轻松的掌握各种设计模式.继上篇博 ...
- 摘录-Mybatis - Integer值为0的数据 return false
Mybatis在进行<if test="status != null and status != ''">判空操作时,如果status为0的时候,该判断条件的值为fal ...
- PatentTips -- 一种在CoAP网络中注册的方法及装置
技术领域 [0001] 本发明涉及一种在CoAP网络中注册的方法及装置,属于网络通信技术领域. 背景技术 [0002] (Internet of Things,物联网)作为新一代的信息技术,越来越受到 ...
- CentOS 7安装fcitx中文输入法
安装过程例如以下: 1.增加EPEL源 EPEL7差点儿是CentOS必备的源: sudo yum install epel-release 2.加入mosquito-myrepo源 mosquito ...
- 最新国内外可用SVN托管仓库有哪些
最新国内外可用SVN托管仓库哪些 一.总结 一句话总结:用SVNBucket和SourceForge 二.最新国内外可用SVN托管仓库推荐 这几年很多SVN托管平台都基本不维护或者直接关闭了,我翻遍了 ...
- PatentTips – CoAP Segment size determination
BACKGROUND OF THE INVENTION The subject matter disclosed herein relates to routing data through a ne ...
- [转]erlang ranch
一. ranch app启动: ranch_sup -> ranch_server % 创建ets, 并提供接口给其他进程读写 二. 启动diy app (监听模块: 用ranch_tcp -& ...
- 欢迎阅读 Erlang OTP 设计原理文档
http://erldoc.com/doc/otp-design-principles/index.html 原文: OTP Design Principles 翻译: ShiningRay 有任何问 ...
- NOIP 模拟 box - 费用流 / 匈牙利
题目大意: 给出n(\(\le 200\))个盒子,第i个盒子长\(x_i\),宽\(y_i\),一个盒子可以放入长宽都大于等于它的盒子里,并且每个盒子里只能放入一个盒子(可以嵌套),嵌套的盒子的占地 ...
- 【hdu2457】ac自动机 + dp
传送门 题目大意: 给你一个字符主串和很多病毒串,要求更改最少的字符使得没有一个病毒串是主串的子串. 题解: ac自动机 + dp,用病毒串建好ac自动机,有毒的末尾flag置为true 构建fail ...