winform程序中httpwebrequest请求在第三次请求的时候离奇timeout
我把一段xml通过http post给一个服务器,在前两次post的时候都会成功,也会返回正确的结果,但是每到第三次post的时候就会出错,说是timeout了,第三次的发送的内容格式和前两次都是一样,不明白为什么偏偏第三次就失败。请高手解答,下面是我的代码:
public HttpWebResponse HttpPost(XmlDocument postInfo)
{
HttpWebRequest objHttpWebRequest;
HttpWebResponse objHttpWebResponse = null;
Stream objRequestStream = null;
//Creates an HttpWebRequest for the specified URL.
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(mEndPoint);
try
{
//---------- Start HttpRequest
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/xml; encoding=utf-8 ";
objHttpWebRequest.KeepAlive = true;
objHttpWebRequest.Accept = "application/xml";
//objHttpWebRequest.AutomaticDecompression = DecompressionMethods.;
objHttpWebRequest.Headers.Add(RestConnection.SESSION_ID, mSessionId);
objHttpWebRequest.Timeout = mTimeout;//Timeout.Infinite;
//Set HttpWebRequest properties
if (postInfo != null)
{
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(postInfo.InnerXml);
objHttpWebRequest.ContentLength = bytes.Length;
objRequestStream = objHttpWebRequest.GetRequestStream();//第三次就会在这里抛错说是timeout了
//Writes a sequence of bytes to the current stream
objRequestStream.Write(bytes, 0, bytes.Length);
//Close stream
objRequestStream.Close();
}
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
//---------- Start HttpResponse
if (objHttpWebResponse.StatusCode != HttpStatusCode.BadRequest)
{
return objHttpWebResponse;
}
}
catch (WebException we)
{
//TODO: Add custom exception handling
throw new Exception(we.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
objRequestStream.Close();
objRequestStream = null;
objHttpWebRequest = null;
}
return null;
}