Http Post WebRequest getting timed out

2019-06-02 06:22发布

问题:

HttpWebRequest request = WebRequest.CreateHttp("http://www.ooredoo.mv/edirectory");
request.Method = "POST";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Referer = "http://www.ooredoo.mv/edirectory";
request.KeepAlive = true;
request.Headers.Add(HttpRequestHeader.Cookie, "PHPSESSID=cu722i6j4kkpm8d6lbp9e3f9t7; BNES_PHPSESSID=G4HHdpBzuOW4x+zrBTytg7K3VC/EPsvp1s8seSUzAXF9wXEIdzNA4a58Scih6NYP6qjl8ndRyTuux1ohMhhbww==");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
request.ContentType = "application/x-www-form-urlencoded";

string postString = "number=9100000&code=Gfdf&submit=";
byte[] postData = Encoding.ASCII.GetBytes(postString);
request.ContentLength = postData.Length;

using (Stream stream = request.GetRequestStream())
{
    stream.Write(postData, 0, postData.Length);
    stream.Flush();
}

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {//Timeout
    //Some code which uses 'response'
}

But it keeps getting timeout at the request.GetResponse() and gives me the exception : Unhandled Exception: System.Net.WebException: The request was aborted: The operation has timed out.

I am able to send the same request using firefox without any problem and I don't get a timeout. So this must be a problem with HttpWebRequest
Not sure what's wrong with my code. If you want, you can check the website yourself.

Edit: I used wireshark and after it makes the post request. It shows 5176 380.957168000 192.168.1.2 202.153.85.30 TCP 87 [TCP Retransmission] 56462→80 [PSH, ACK] Seq=637 Ack=26 Win=66048 Len=33[Reassembly error, protocol TCP: New fragment overlaps old data (retransmission?)]

If I do not write to the request stream, the server returns a 404 error. So its not a problem with my internet connection.