简体   繁体   English

HttpWebRequest.GetResponse 无论如何总是超时

[英]HttpWebRequest.GetResponse always times out no matter what

I am unable to make a post request to a URL using HttpWebRequest.我无法使用 HttpWebRequest 向 URL 发出发布请求。 I have applied all the suggestions I have seen online.我已经应用了我在网上看到的所有建议。 Most answers on SO suggest specifying a user agent. SO 上的大多数答案都建议指定用户代理。 I have done that and provided a timeout as well but still I get a timeout.我已经这样做了,并提供了超时,但我仍然超时。 I saw another post where it was mentioned to specify readwritetimeout instead.我看到另一篇文章,其中提到要指定 readwritetimeout。 Still I get the same response, "Server Timed out".我仍然得到相同的响应,“服务器超时”。 Making the post using a browser works within a second, but using code in C# gives the timeout issue.使用浏览器制作帖子在一秒钟内即可工作,但使用 C# 中的代码会导致超时问题。 Here is my code:这是我的代码:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); 
        request.ContentType = "application/x-www-form-urlencoded";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2288.6 Safari/537.36";
        request.Method = "POST";
        request.KeepAlive = true;
        request.CookieContainer = new CookieContainer();
        request.Accept = "*/*";
        request.ReadWriteTimeout = System.Threading.Timeout.Infinite;
        request.Headers.Add("Accept-Encoding: gzip, deflate");
        request.Headers.Add("Accept-Language: en-US");
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string Response = reader.ReadToEnd();

I am getting a timeout at this line:我在这一行超时:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Am I missing something here?我在这里错过了什么吗?

Here is the log after switching on diagonostics in the config.这是在配置中打开对角线后的日志。

System.Net Verbose: 0 : [0280] WebRequest::Create(http://mysite.url.here)
System.Net Verbose: 0 : [0280] HttpWebRequest#13062350::HttpWebRequest(http://mysite.url.here)
System.Net Information: 0 : [0280] Current OS installation type is 'Client'.
System.Net Information: 0 : [0280] RAS supported: True
System.Net Verbose: 0 : [0280] Exiting HttpWebRequest#13062350::HttpWebRequest()

System.Net Verbose: 0 : [0280] Exiting WebRequest::Create()     -> HttpWebRequest#13062350
System.Net Error: 0 : [0280] Can't retrieve proxy settings for Uri 'http://mysite.url.here'. Error code: 12180.
System.Net Verbose: 0 : [0280] ServicePoint#10366524::ServicePoint(http://mysite.url.here:80)
System.Net Information: 0 : [0280] Associating HttpWebRequest#13062350 with ServicePoint#10366524
System.Net Verbose: 0 : [0280] HttpWebRequest#13062350::GetRequestStream()
System.Net Information: 0 : [0280] Associating Connection#63840421 with HttpWebRequest#13062350
System.Net Information: 0 : [0280] Connection#63840421 - Created connection from 192.168.1.5:13902 to 199.249.234.200:80.
System.Net Information: 0 : [0280] Associating HttpWebRequest#13062350 with ConnectStream#54246671
System.Net Information: 0 : [0280] HttpWebRequest#13062350 - Request: POST /Post-url-here HTTP/1.1

System.Net Information: 0 : [0280] ConnectStream#54246671 - Sending headers
{
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, likeGecko) Chrome/42.0.2288.6 Safari/537.36
Accept-Encoding: gzip, deflate
Accept-Language: en-US
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Host: www.powersportrider.com
Content-Length: 101
Expect: 100-continue
Connection: Keep-Alive
}.

System.Net Verbose: 0 : [0280] Exiting ConnectStream#54246671::Write()
System.Net Verbose: 0 : [0280] ConnectStream#54246671::Close()
System.Net Verbose: 0 : [0280] Exiting ConnectStream#54246671::Close()
System.Net Verbose: 0 : [0280] HttpWebRequest#13062350::GetResponse()
System.Net Information: 0 : [0280] Connection#63840421 - Received status line: V
ersion=1.1, StatusCode=100, StatusDescription=Continue.
System.Net Information: 0 : [0280] Connection#63840421 - Received headers
{

}.


System.Net Verbose: 0 : [8668] HttpWebRequest#13062350::Abort(The operation hastimed out)
System.Net Verbose: 0 : [8668] Exiting HttpWebRequest#13062350::Abort()
System.Net Error: 0 : [0280] Exception in HttpWebRequest#13062350:: - The operation has timed out.
System.Net Error: 0 : [0280] Exception in HttpWebRequest#13062350::GetResponse - The operation has timed out.
System.Net Error: 0 : [0280] Exception in AppDomain#12036987::UnhandledException
Handler - The operation has timed out.   at System.Net.HttpWebRequest.GetResponse()

Turns out all I need to do to get this to work was add the following lines before the request.GetResponse() call原来我需要做的就是在request.GetResponse()调用之前添加以下几行

request.ServicePoint.Expect100Continue = false;
request.ProtocolVersion = HttpVersion.Version11;

Hope the hours I spent on this helps someone else out quicker.希望我花在这上面的时间可以帮助其他人更快地解决问题。

I had the very same issue.我有同样的问题。 For me the fix was as simple as wrapping the HttpWebResponse code in using block.对我来说,修复就像在 using 块中包装 HttpWebResponse 代码一样简单。

using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
    // Do your processings here....
}

This issue usually happens if you make several requests to the same host, and WebResponse is not disposed properly.如果您向同一主机发出多个请求,并且未正确处理WebResponse则通常会发生此问题。 That is why the using block comes here as a savior.这就是 using 块作为救世主来到这里的原因。

I was getting this issue because one of the parameters was getting sent in capital letters and it was supposed to be in small letters ( Silly mistake ).我遇到这个问题是因为其中一个参数是以大写字母发送的,它应该是小写字母(愚蠢的错误)。 Hope this may help somebody doing same mistake as me.希望这可以帮助与我犯同样错误的人。

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

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