简体   繁体   English

在 Asp.Net Core 的 HttpWebRequest 上获取“操作已超时”消息

[英]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();
  1. What is the size of response data, if it is huge, you may get timeout exception.响应数据的大小是多少,如果很大,可能会出现超时异常。

  2. What is health of endpoint.什么是端点的健康状况。 Are you seeing any request time out issues, when you try to ping from command prompt console当您尝试从命令提示符控制台 ping 时,您是否看到任何请求超时问题

  3. 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.

相关问题 HttpWebRequest - 操作已超时 - HttpWebRequest - The operation has timed out 等待操作超时 ASP.NET - The wait operation timed out ASP.NET HttpWebRequest请求被中止:操作已超时 - HttpWebRequest The request was aborted: The operation has timed out HttpWebRequest.GetResponse操作已超时 - HttpWebRequest.GetResponse Operation has timed out 从亚马逊 s3 读取图像和视频时,在 System.Net.HttpWebRequest.GetResponse() 处获取 WebException“操作已超时” - Getting WebException “The operation has timed out” at System.Net.HttpWebRequest.GetResponse() when reading images and video from amazon s3 使用asp.net和smtp.gmail.com发送电子邮件时,操作已超时 - Operation has timed out when sending email using asp.net and smtp.gmail.com 操作已超时。 使用C#ASP.net通过smtp发送电子邮件时 - The operation has timed out. while sending email through smtp using C# ASP.net System.Net.WebException:操作在 System.Net.HttpWebRequest.GetResponse 超时 - System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse 我正在获取WebException:“操作已超时”立即在HttpWebRequest.GetResponse()上 - I am getting WebException: “The operation has timed out” immediately on HttpWebRequest.GetResponse() Asp.net Crawler Webresponse操作超时 - Asp.net Crawler Webresponse Operation Timed out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM