[英]Getting “The operation has timed out” message on HttpWebRequest in Asp.Net Core
I'm trying to make a request to a webpage and am getting an error that "The operation has timed out".我正在尝试向网页发出请求,并收到“操作已超时”的错误。 I've tried overriding the default timeout with no luck.我尝试过没有运气覆盖默认超时。 The fact that the same url works fine in the browser, and that my code works fine for other domains suggests maybe I'm missing headers or something else?事实上,相同的 url 在浏览器中运行良好,而我的代码在其他域中运行良好,这表明我可能缺少标题或其他内容? Could anyone suggest what to add to the request to get past the timeout issue?谁能建议在请求中添加什么来解决超时问题?
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.mrporter.com/en-gb");
request.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36");
var wresp = (HttpWebResponse)request.GetResponse();
What is the size of response data, if it is huge, you may get timeout exception.响应数据的大小是多少,如果很大,可能会出现超时异常。
What is health of endpoint.什么是端点的健康状况。 Are you seeing any request time out issues, when you try to ping from command prompt console当您尝试从命令提示符控制台 ping 时,您是否看到任何请求超时问题
increase request timeout value增加请求超时值
request.TimeOut =3600;// milli second value request.TimeOut =3600;//毫秒值
Perhaps you are not following the redirect being returned?也许您没有遵循返回的重定向? Setting this property to true will instruct the web request to do so, HttpWebRequest.AllowAutoRedirect
.将此属性设置为 true 将指示 web 请求这样做, HttpWebRequest.AllowAutoRedirect
。 I did the following to check this out.我做了以下检查。
tried this at the Linux command prompt (http is like curl but I just like it better)在 Linux 命令提示符下试过这个(http 就像 curl 但我更喜欢它)
http --verbose --follow --timeout 3600 GET 'https://www.mrporter.com/en-gb'
The verbose option gives me both the request and the response?详细选项给了我请求和响应? The http
command is sending this as the request: http
命令将其作为请求发送:
GET /en-gb HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: www.mrporter.com
User-Agent: HTTPie/1.0.3
The initial response from the host includes (with a lot of other stuff):主机的初始响应包括(还有很多其他的东西):
HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 0
Date: Tue, 23 Feb 2021 14:09:22 GMT
Location: /en-gb/
The http
command then follows the redirect like this (again, with details left out to make things easier): http
命令然后像这样跟随重定向(同样,省略了细节以使事情变得更容易):
GET /en-gb/ HTTP/1.1
Accept: */*
And it returns the screen fine.它可以很好地返回屏幕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.