C#HttpWebRequest响应超时,在浏览器中工作

时间:2017-03-26 18:33:12

标签: c# visual-studio timeout httpwebrequest

我花了几个小时调试代码,和Fiddler一起玩,谷歌搜索,但仍然没有运气,所以希望你能帮助我。

我正在尝试获取http://www.finishline.com的来源。问题是,HttpWebRequest在某些地区(如斯洛伐克这里)有效,但在美国并没有我需要实现的目标。

对于美国,request.GetResponse()只是超时。我尝试了无数的标头组合,但没有成功。你能帮忙吗?谢谢

var request = (HttpWebRequest)WebRequest.Create("http://www.finishline.com");

            request.CookieContainer = new CookieContainer();
            request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
            request.AutomaticDecompression = (DecompressionMethods.GZip | DecompressionMethods.Deflate);
            request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Accept = " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.Headers.Add("Upgrade-Insecure-Requests", "1");                
            request.Headers.Add("Accept-Language", "sk,cs;q=0.8,en-US;q=0.5,en;q=0.3");
            request.KeepAlive = true;
            request.Headers.Add("Cache-Control", "max-age=0");

            var responseText = "";
            using (var response = request.GetResponse())
            {
                var httpWebResponse = response.GetResponseStream();

                using (var sr = new StreamReader(httpWebResponse))
                {
                    responseText = sr.ReadToEnd();
                }
            }

1 个答案:

答案 0 :(得分:0)

有两种超时。客户端超时和服务器超时。你有没有试过这样的事情:

request.Timeout = Timeout.Infinite;
request.KeepAlive = true;

因此,您的GetResponse永远不会超时,您可以查看它。

或者

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
var httpWebResponse = response.GetResponseStream();

            using (var sr = new StreamReader(httpWebResponse))
            {
                responseText = sr.ReadToEnd();
            }
}

尝试使用此方法解决问题。