HttpWebRequest receives "WebException: The request timed out"
<p>After endless research and testing of different combinations, I'm clueless right now.</p>

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

<p>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.</p>

<p>For testing purposes (self-signed SSL-Cert) I have this:</p>

<pre><code>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; 
    };
</code></pre>

<ul>
<li>If I enter the URL in my web-browser (Chrome), I get a response.</li>
<li>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)</li>
<li>If I run my code below with <code>System.Text.Encoding.UTF8.GetBytes("")</code> everything works (except I cannot send data and therefore receive what I want)</li>
</ul>

<p>Here's the code I'm using.</p>

<pre><code>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 ();
</code></pre>

<p>The exact error (btw, all this happens in the Unity3D editor):</p>

<p><code>WebException: The request timed out
System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult)
System.Net.HttpWebRequest.GetResponse ()</code></p>

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

<p>Thanks and all the best,
Kruegbert</p>

<hr>

<p><strong>..::Addendum</strong></p>

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