简体   繁体   English

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

[英]C# HttpWebRequest response times out eventough works in browser

I spent several hours debuging the code, playing with Fiddler and googling, but still no luck, so hopefully you will help me. 我花了几个小时调试代码,玩Fiddler和谷歌搜索,但仍然没有运气,所以希望你能帮助我。

I am trying to get the source of http://www.finishline.com . 我想获得http://www.finishline.com的来源。 The catch is, the HttpWebRequest works in some regions (like here in Slovakia), but doesn't work in USA what I need to achieve. 问题是,HttpWebRequest在某些地区(如斯洛伐克这里)工作,但在美国无法实现我需要实现的目标。

For USA the request.GetResponse() just time outs. 对于美国,request.GetResponse()只是超时。 I have tried countless headers combinations, but without success. 我尝试了无数的标头组合,但没有成功。 Can you please help? 你能帮忙吗? Thank you 谢谢

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();
                }
            }

There are two kind of timeouts. 有两种超时。 Client timeout and server timeout. 客户端超时和服务器超时。 Have you tried doing something like this: 你有没有试过这样的事情:

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

So, your GetResponse never get timedout you can check with it. 因此,您的GetResponse永远不会得到时间,您可以检查它。

Or 要么

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

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

Try this method to fix your issue. 尝试使用此方法来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM