使用Tor代理时,服务器发生了协议违规。部分=ResponseStatusLine。

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

服务器违反了协议。 Section=ResponseStatusLine

我已经尝试在网上寻找解决方案,并找到了3个主要的解决方案:
  1. Add to Web.config.

    <system.net>
      <settings>
        <httpWebRequest useUnsafeHeaderParsing="true"/>
      </settings>
    </system.net>`
    
  2. Add the line: webRequest.ProtocolVersion = HttpVersion.Version10; to the code.

  3. Add the line request.ServicePoint.Expect100Continue = false; to the code.

列出的解决方案都无法改变错误信息。

这是请求代码:

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;

有人能帮我找到解决这个问题的方法吗?
2个回答

3
你可以通过查看异常的Response属性,然后检查StatusDescriptionStatusCode属性来获取有关你正在收到的异常(实际上是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 的示例以获取更多详细信息。

3
我尝试添加这些行,但状态不是协议错误,而是服务器协议违规。 - Imri Barr
3
确实,对于我的异常,响应为 null :( - Zhaph - Ben Duguid
1
这里有同样的问题。响应为空。 - tnktnk

0

我曾经遇到过同样的问题。httpwebrequest getresponse方法总是返回错误的协议违规,我是这样解决问题的:

首先,我使用xml com对象代替xdocument或xmldocument。

这个com对象有几个版本,包括微软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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接