doulu0266 2016-06-24 19:55
浏览 472
已采纳

HttpWebRequest收到“WebException:请求超时”

After endless research and testing of different combinations, I'm clueless right now.

I receive an WebException: The request timed out only if I my byteArray gets filled by something else than System.Text.Encoding.UTF8.GetBytes(""). (Like "hello")

The server setup is a https-request to a Google Load Balancer, which communicates with the backend via HTTP. The backend is an Apache with PHP.

For testing purposes (self-signed SSL-Cert) I have this:

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; 
    };
  • If I enter the URL in my web-browser (Chrome), I get a response.
  • If I use the HTTP-requester from Mozilla with or without content to send, I get the correct response data (after adding an SSL-Security exception)
  • If I run my code below with System.Text.Encoding.UTF8.GetBytes("") everything works (except I cannot send data and therefore receive what I want)

Here's the code I'm using.

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 ();

The exact error (btw, all this happens in the Unity3D editor):

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

So why on earth is it not working, once there is something the GetRequestStream has to write?

Thanks and all the best, Kruegbert


..::Addendum

  • if I increase the timeout, it just takes longer until the same msg appears.
  • If I write webRequest.ContentLength = byteArray.Length+1 I receive a response, but it's a WebException error: ProtocolError
  • If I write webRequest.ContentLength = byteArray.Length-1 I get the ProtocolViolationException
  • I already tried the same with try/catch/using resulting in the same behaviour
  • 写回答

1条回答 默认 最新

  • doukuang6795 2016-06-29 09:44
    关注

    I figured out, why it was not working - still I don't know why it behaves like this. (Maybe a UnityEditor thing)

    I added

    webRequest.ProtocolVersion = HttpVersion.Version10;
    

    and everything worked. No more timeout errors. And yes webRequest.ProtocolVersion = HttpVersion.Version11; results in the timeout error.

    However, making a HttpRequest from the web succeeds with either of these: HTTP/1.1, HTTP/1.0 (with Host header), HTTP/1.0 (without Host header)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况