服务器提交了协议违规。 Section =使用tor代理时的ResponseStatusLine

我正在尝试使用我的asp.net应用程序使用tor代理发送httpwebrequest,并在调用webresponse.GetResponse()方法时收到此错误消息:

服务器提交了协议违规。 第= ResponseStatusLine

我尝试在网上搜索解决方案,我找到了3个主要解决方案:

  1. 添加到Web.config。

        ` 
  2. 添加以下行: webRequest.ProtocolVersion = HttpVersion.Version10; 到代码。

  3. 添加行request.ServicePoint.Expect100Continue = false; 到代码。

列出的每个解决方案都没有改变错误消息。

这是请求代码:

 WebRequest.DefaultWebProxy = new WebRequest("127.0.0.1:9051"); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.CookieContainer = new CookieContainer(); webRequest.ProtocolVersion = HttpVersion.Version10; webRequest.KeepAlive = false; webRequest.Method = "GET"; webRequest.ContentType = "application/x-www-form-urlencoded"; webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()); string html = streamReader.ReadToEnd(); webResponse.Close(); return html; 

任何人都可以帮我找到解决方案吗?

通过查看exception的Response属性,然后检查StatusDescriptionStatusCode属性,您可以获得有关您获得的exception的更多详细信息,这实际上是WebException 。 这将有助于您获得有关错误的更多详细信息,并希望指出您正确的方向。

像这样的东西:

  catch(WebException e) { if(e.Status == WebExceptionStatus.ProtocolError) { Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode); Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription); } } 

另外,请查看MSDN上的WebException.Status示例以获取更多详细信息

我有同样的问题。 httpwebrequest getresponse方法总是返回错误协议vialotion,我这样解决了问题;

首先,我使用xml com对象而不是xdocument或xmldocument。

这个com对象有一些版本的Microsft XML,v3.0-v5.0-v6.0。 我使用的是v6.0。

 MSXML2.DOMDocument60Class doc = new MSXML2.DOMDocument60Class(); doc.setProperty("ServerHTTPRequest",true); doc.setProperty("ProhibitDTD", false); doc.async = false; doc.load(extURL); if (doc.parseError.errorCode != 0) { // error } else { // do stuff }