【问题标题】:HttpWebRequest receives "WebException: The request timed out"HttpWebRequest 收到“WebException:请求超时”
【发布时间】:2016-06-24 19:55:58
【问题描述】:

经过对不同组合的无尽研究和测试,我现在一无所知。

只有当我的byteArraySystem.Text.Encoding.UTF8.GetBytes("") 以外的其他东西填充时,我才会收到WebException: The request timed out。 (比如“你好”)

服务器设置是对 Google 负载均衡器的 https 请求,它通过 HTTP 与后端通信。后端是带有 PHP 的 Apache。

出于测试目的(自签名 SSL-Cert)我有这个:

System.Net.ServicePointManager.ServerCertificateValidationCallback = 
        delegate (object s,  
            System.Security.Cryptography.X509Certificates.X509Certificate certificate,  
            System.Security.Cryptography.X509Certificates.X509Chain chain, 
            System.Net.Security.SslPolicyErrors sslPolicyErrors){ 
        return true; 
    };
  • 如果我在网络浏览器 (Chrome) 中输入 URL,我会收到响应。
  • 如果我使用来自 Mozilla 的 HTTP 请求器,无论是否发送内容,我都会得到正确的响应数据(在添加 SSL 安全例外之后)
  • 如果我在下面使用System.Text.Encoding.UTF8.GetBytes("") 运行我的代码,一切正常(除了我无法发送数据并因此接收我想要的)

这是我正在使用的代码。

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://someurl.com/some.php");
webRequest.Proxy = null;
webRequest.Credentials = CredentialCache.DefaultCredentials;
webRequest.Method = "POST";
webRequest.Timeout = 3000;

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("someData");  //works if empty
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;

Stream postData = webRequest.GetRequestStream();
postData.Write(byteArray, 0, byteArray.Length);
postData.Close();

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); //ERROR MESSAGE
Stream dataStream = webResponse.GetResponseStream();
reader = new StreamReader(dataStream);
string data = reader.ReadToEnd(); //output data

reader.Close ();
dataStream.Close ();
webResponse.Close ();

确切的错误(顺便说一句,这一切都发生在 Unity3D 编辑器中):

WebException: The request timed out System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) System.Net.HttpWebRequest.GetResponse ()

那么,一旦 GetRequestStream 必须写一些东西,它到底为什么不工作?

谢谢,一切顺利, 克鲁格伯特


..::附录

  • 如果我增加超时时间,则需要更长的时间才能出现相同的消息。
  • 如果我写 webRequest.ContentLength = byteArray.Length+1 我会收到响应,但这是一个 WebException 错误:ProtocolError
  • 如果我写webRequest.ContentLength = byteArray.Length-1,我会得到ProtocolViolationException
  • 我已经尝试过使用 try/catch/using 导致相同的行为

【问题讨论】:

  • 只是为了确定,希望您尝试过webRequest.ContentLength = byteArray.Length
  • 是的,我做到了。后来我只是在试验它。

标签: c# php ssl unity3d httpwebrequest


【解决方案1】:

我想通了,为什么它不起作用 - 我仍然不知道它为什么会这样。 (也许是 UnityEditor 的东西)

我加了

webRequest.ProtocolVersion = HttpVersion.Version10;

一切正常。没有更多的超时错误。是的webRequest.ProtocolVersion = HttpVersion.Version11; 导致超时错误。

但是,通过以下任一方式从 Web 发出 HttpRequest 会成功:HTTP/1.1HTTP/1.0 (with Host header)HTTP/1.0 (without Host header)

【讨论】:

  • 只是为了更新我使用webRequest.ProtocolVersion = HttpVersion.Version11; 的答案,我必须添加webRequest.ServicePoint.Expect100Continue = false;,因为服务器不支持HTTP/1.1 100 继续响应。
  • 这个修复也适用于我。但是,我真的很想知道为什么?更多信息:我也在 Unity 中工作,但这发生在我的 iPhone 上,而不是在编辑器中,并且只有在应用程序运行了一段时间之后。后端是 Amazon CloudFront。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2013-07-23
  • 2019-02-14
  • 1970-01-01
  • 2012-08-28
相关资源
最近更新 更多