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

Imr*_*arr 8 c# asp.net proxy httpwebrequest tor

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

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

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

  1. 添加到Web.config.

    <system.net>
      <settings>
        <httpWebRequest useUnsafeHeaderParsing="true"/>
      </settings>
    </system.net>`
    
    Run Code Online (Sandbox Code Playgroud)
  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;
Run Code Online (Sandbox Code Playgroud)

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

Pun*_*ora 3

通过查看异常的属性,然后检查和属性,您可以获得有关所获取的异常(实际上是WebException)的更多详细信息。这将帮助您获得有关错误的更多详细信息,并希望为您指明正确的方向。ResponseStatusDescriptionStatusCode

像这样的东西:

    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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

  • 我尝试添加这些行,但状态不是协议错误,而是 ServerProtocolViolation。 (3认同)
  • 事实上,对于我的例外,响应为空:( (3认同)