当请求具有Gzip压缩的页面时,我收到了很多以下错误:
System.IO.InvalidDataException:GZip页脚中的CRC与从解压缩数据计算的CRC不匹配
我正在使用本机GZipStream进行解压缩,正在寻找解决此问题的方法.考虑到这一点,是否有解决这个或另一个GZip库(免费?)的工作,它将正确处理这个问题?
我正在验证webResponse ContentEncoding是否为GZIP
更新5/11 简化的snippit
//Caller
public void SOSampleGet(string url)
{
// Initialize the WebRequest.
webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.KeepAlive = true;
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
webRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
webRequest.Referer = WebUtil.GetDomain(url);
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
using (Stream stream = GetStreamForResponse(webResponse, READTIMEOUT_CONST))
{
//use stream
}
}
//Method
private static Stream GetStreamForResponse(HttpWebResponse webResponse, int readTimeOut)
{
Stream stream;
switch (webResponse.ContentEncoding.ToUpperInvariant())
{
case "GZIP":
stream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
break;
case "DEFLATE":
stream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress);
break;
default: …Run Code Online (Sandbox Code Playgroud) 有没有一种快速方法来解压缩使用WebClient.DownloadString()方法下载的gzip响应?您对如何使用WebClient处理gzip响应有任何建议吗?
想要解压缩从 API 获取的 GZipped 响应。尝试了下面的代码,它总是返回类似:-
\n\n\\u001f\xef\xbf\xbd\\b\\0\\0\\0\\0\\0\\0\\0\xef\xbf\xbdY]o........\nRun Code Online (Sandbox Code Playgroud)\n\n我的代码是:
\n\n private string GetResponse(string sData, string sUrl)\n {\n try\n {\n string script = null;\n try\n {\n string urlStr = @"" + sUrl + "?param=" + sData;\n\n Uri url = new Uri(urlStr, UriKind.Absolute);\n\n HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);\n request.Method = "GET";\n request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;\n\n using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())\n using (StreamReader reader = new StreamReader(response.GetResponseStream()))\n {\n script = reader.ReadToEnd();\n } \n }\n catch (System.Net.Sockets.SocketException)\n {\n // The remote site is currently …Run Code Online (Sandbox Code Playgroud) 我有这段代码可以从 URL 获取页面 HTML,但是响应内容看起来是经过编码的。
代码:
HttpWebRequest xhr = (HttpWebRequest) WebRequest.Create(new Uri("https://www.youtube.com/watch?v=_Ewh75YGIGQ"));
xhr.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
//xhr.CookieContainer = request.Account.CookieContainer;
xhr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
xhr.Headers["Accept-Encoding"] = "gzip, deflate, br";
xhr.Headers["Accept-Language"] = "en-US,en;q=0.5";
xhr.Headers["Upgrade-Insecure-Requests"] = "1";
xhr.KeepAlive = true;
xhr.UserAgent = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
xhr.Host = "www.youtube.com";
xhr.Referer = "https://www.youtube.com/watch?v=6aCpYxzRkf4";
var response = xhr.GetResponse();
string html;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
html = reader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)
这些是响应标头:
X-XSS-Protection: 1; mode=block; report=https://www.google.com/appserve/security-bugs/log/youtube
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Strict-Transport-Security: max-age=31536000
Content-Encoding: …Run Code Online (Sandbox Code Playgroud)