使用HttpWebRequest访问时出错:ServerProtocolViolation
一直运行正常的一个网站,其中一个模块使用HttpWebRequest来访问其他网页获取信息,今天突然发生错误,在执行HttpWebResponse response=(HttpWebResponse)request.GetResponse()时发生异常:
“基础连接已经关闭: 该服务器提交了 HTTP 协议冲突。”,这时的WebExceptionStatus为“ServerProtocolViolation(此服务器响应不是有效的 HTTP 响应),我直接用浏览器访问对方网站,可以正常显示,没有任何问题。
在Google上搜索发现了许多类似的问题,都没有解决方案,不知是不是.NET的BUG啊?
例如:http://thedotnet.com/nntp/332305/showpost.aspx
我的代码如下:
HttpWebRequest request=(HttpWebRequest)WebRequest.Create(@"http://www.target.com/target.asp");
request.ProtocolVersion=HttpVersion.Version11;
request.Method="POST";
request.ContentType="text/plain";
request.KeepAlive = true;
request.ContentLength=data.Length;
try
{
Stream newStream=request.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Close();
HttpWebResponse response=(HttpWebResponse)request.GetResponse(); //异常
...
}
catch
{
}
各位高手,帮帮我。