【发布时间】:2016-06-24 19:55:58
【问题描述】:
经过对不同组合的无尽研究和测试,我现在一无所知。
只有当我的byteArray 被System.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