WebRequest 未返回 HTML
WebRequest not returning HTML
我想加载此 http://www.yellowpages.ae/categories-by-alphabet/h.html url,但它 returns 无效
在某些问题中,我听说过添加 Cookie 容器,但它已经存在于我的代码中。
var MainUrl = "http://www.yellowpages.ae/categories-by-alphabet/h.html";
HtmlWeb web = new HtmlWeb();
web.PreRequest += request =>
{
request.CookieContainer = new System.Net.CookieContainer();
return true;
};
web.CacheOnly = false;
var doc = web.Load(MainUrl);
网站在浏览器中打开完全正常。
您需要 CookieCollection
来获取 cookie 并在 HtmlWeb
中将 UseCookie
设置为 true
。
CookieCollection cookieCollection = null;
var web = new HtmlWeb
{
//AutoDetectEncoding = true,
UseCookies = true,
CacheOnly = false,
PreRequest = request =>
{
if (cookieCollection != null && cookieCollection.Count > 0)
request.CookieContainer.Add(cookieCollection);
return true;
},
PostResponse = (request, response) => { cookieCollection = response.Cookies; }
};
var doc = web.Load("https://www.google.com");
我怀疑是cookie问题。看起来像 gzip 加密,因为当我试图获取页面时,除了乱码之外什么也没有。如果这是一个 cookie 问题,响应应该 return 一个错误说明。无论如何。这是我对你的问题的解决方案。
public static void Main(string[] args)
{
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.yellowpages.ae/categories-by-alphabet/h.html");
request.Method = "GET";
request.ContentType = "text/html;charset=utf-8";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
doc.Load(stream, Encoding.GetEncoding("utf-8"));
}
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine(doc.DocumentNode.InnerHtml);
Console.ReadKey();
}
它所做的只是 decrypts/extracts 我们收到的 gzip 消息。
我怎么知道你问的是 GZIP?来自调试器的响应流表示 ContentEncoding 是 gzip。
基本上只需添加:
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
你的代码很好。
我想加载此 http://www.yellowpages.ae/categories-by-alphabet/h.html url,但它 returns 无效
在某些问题中,我听说过添加 Cookie 容器,但它已经存在于我的代码中。
var MainUrl = "http://www.yellowpages.ae/categories-by-alphabet/h.html";
HtmlWeb web = new HtmlWeb();
web.PreRequest += request =>
{
request.CookieContainer = new System.Net.CookieContainer();
return true;
};
web.CacheOnly = false;
var doc = web.Load(MainUrl);
网站在浏览器中打开完全正常。
您需要 CookieCollection
来获取 cookie 并在 HtmlWeb
中将 UseCookie
设置为 true
。
CookieCollection cookieCollection = null;
var web = new HtmlWeb
{
//AutoDetectEncoding = true,
UseCookies = true,
CacheOnly = false,
PreRequest = request =>
{
if (cookieCollection != null && cookieCollection.Count > 0)
request.CookieContainer.Add(cookieCollection);
return true;
},
PostResponse = (request, response) => { cookieCollection = response.Cookies; }
};
var doc = web.Load("https://www.google.com");
我怀疑是cookie问题。看起来像 gzip 加密,因为当我试图获取页面时,除了乱码之外什么也没有。如果这是一个 cookie 问题,响应应该 return 一个错误说明。无论如何。这是我对你的问题的解决方案。
public static void Main(string[] args)
{
HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.yellowpages.ae/categories-by-alphabet/h.html");
request.Method = "GET";
request.ContentType = "text/html;charset=utf-8";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var response = (HttpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
doc.Load(stream, Encoding.GetEncoding("utf-8"));
}
}
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine(doc.DocumentNode.InnerHtml);
Console.ReadKey();
}
它所做的只是 decrypts/extracts 我们收到的 gzip 消息。 我怎么知道你问的是 GZIP?来自调试器的响应流表示 ContentEncoding 是 gzip。
基本上只需添加:
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
你的代码很好。