winform程序中httpwebrequest请求在第三次请求的时候离奇timeout

reejayyang 2010-01-04 02:33:53
我把一段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;
}
...全文
224 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
彖爻之辞 2011-11-08
  • 打赏
  • 举报
回复
objHttpWebRequest.Headers.Add(RestConnection.SESSION_ID, mSessionId);
中的值是什么意思,如何获取
reejayyang 2010-01-05
  • 打赏
  • 举报
回复
我找到解决的方法了,在上一次调用请求之后,调用httpwebrequesta.abort()就好使了。

12,165

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧