我尝试使用下面的代码获取特定网站页面的来源,但失败了。
我可以使用网络浏览器或网络驱动程序在1到2秒钟内获得页面源,但是httpwebrequest失败。
我尝试将实际的Web浏览器cookie放入httpwebrequest中,但也失败了。
(异常-操作已超时)
我想知道为什么它失败了,想从失败中学习。
先感谢您!!。
string Html = String.Empty;
CookieContainer cc = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.coupang.com/");
req.Method = "GET";
req.Host = "www.coupang.com";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3";
req.Headers.Add("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7");
req.CookieContainer = cc;
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
using (StreamReader str = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
{
Html = str.ReadToEnd();
}
答案 0 :(得分:0)
从代码中删除req.Host
应该可以解决问题。
如果未设置Host属性,则HTTP请求中要使用的Host标头值基于请求URI。
您已经在(HttpWebRequest)WebRequest.Create("https://www.coupang.com/")
中设置了URI,因此我认为没有必要再次进行此操作。
结果
请告诉我是否有帮助。