模拟请求(模拟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
学习技术最好的文档就是【官方文档】,没有之一。
还有学习资料【Microsoft Learn】、【CSharp Learn】、【My Note】。
如果,你认为阅读这篇博客让你有些收获,不妨点击一下右下角的【推荐】按钮。
如果,你希望更容易地发现我的新博客,不妨点击一下【关注】。