日期:2014-05-19  浏览次数:21175 次

xml发送请求异常:基础连接已经关闭: 请求已被取消。60分,在线等

string   sendxml   =   "sendxml ";                 int   length   =   sendxml.Length;
System.Text.Encoding   encoding   =   System.Text.Encoding.GetEncoding( "GB2312 ");

string   strUrl= "http://www.jjdjdjdjj.com ";
byte[]   data   =   encoding.GetBytes(sendxml);

//   准备请求...
System.Net.HttpWebRequest   myRequest   =   (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
myRequest.Method   =   "POST ";
myRequest.ContentType   =   "application/x-www-form-urlencoded ";
myRequest.ContentLength   =   data.Length;
System.IO.Stream   newStream   =   myRequest.GetRequestStream();
//   发送数据
newStream.Write(data,   0,   data.Length);
System.Net.HttpWebResponse   res   =   (System.Net.HttpWebResponse)myRequest.GetResponse();
newStream.Close();
Response.Write( "发送数据状态:   "+res.StatusCode);
System.IO.Stream   sRes   =   res.GetResponseStream();
System.IO.StreamReader   sr   =   new   System.IO.StreamReader(sRes);
System.Xml.XmlDocument   dom   =   new   System.Xml.XmlDocument();
dom.Load(sRes);
this.TextBox2.Text   =   (dom.OuterXml).ToString();


程序运行到System.Net.HttpWebResponse   res   =   (System.Net.HttpWebResponse)myRequest.GetResponse();就停住不动,然后过了几十秒抛异常,异常信息如下:基础连接已经关闭:   请求已被取消。

这段程序原来是可以正常运行的,并且已经排除内网不通的情况,外网一直是通的,help!

------解决方案--------------------
一个解决方案是在application.config或web.config文件里加入

<system.net>

<settings>

<httpWebRequest useUnsafeHeaderParsing= "true " />

</settings>

</system.net>

允许系统容忍(tolerant)只以CR或LF结尾的hearder信息