Net Core教程

C# httpclient 解压缩

本文主要是介绍C# httpclient 解压缩,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

string url = "XXX";
HttpClient http = new HttpClient();

var request = new HttpRequestMessage(HttpMethod.Get, url);

request.Content = new StringContent("");

request.Headers.Accept.ParseAdd("application/json, text/javascript, */*;q=0.01");
request.Headers.Referrer = new Uri("XX");
request.Headers.UserAgent.ParseAdd(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
);
request.Headers.Add("X-Requested-With", "XMLHttpRequest");
var response = http.Send(request);
bool IsGzip = response.Content.Headers.ContentEncoding.Contains("gzip");

if (IsGzip)
{
Stream stm = response.Content.ReadAsStreamAsync().Result;
string strHTML = string.Empty;
GZipStream gzip = new GZipStream(stm, CompressionMode.Decompress);//解压缩
using (StreamReader reader = new StreamReader(gzip, Encoding.UTF8))
{
strHTML = reader.ReadToEnd();
}
}
else
{
var outputStr = response.Content.ReadAsStringAsync().Result;
}

这篇关于C# httpclient 解压缩的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!