Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The server committed a protocol violation. Section=ResponseStatusLine when using a tor proxy

I'm trying to send an httpwebrequest using a tor proxy with my asp.net application and I receive this error message when calling the webresponse.GetResponse() method:

The server committed a protocol violation. Section=ResponseStatusLine

I've tried searching for a solution on the web and I found 3 main solutions for this error:

  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.

Each one of the listed solutions didn't change a thing about the error message.

Here's the request 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;

Can anyone help me find a solution for this?

like image 323
Imri Barr Avatar asked Jul 26 '12 17:07

Imri Barr


1 Answers

You can get more details about the exception you are getting which is actually a WebException by looking at the Response property of the exception and then checking the StatusDescription and StatusCode properties. That will help you get more details about the error and hopefully point you in the right direction.

Something like this:

    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);
        }
    }

Also, take a look at WebException.Status example on MSDN to get more details

like image 133
Punit Vora Avatar answered Oct 18 '22 18:10

Punit Vora