2012-07-26 31 views
8

Tôi đang cố gắng để gửi một HttpWebRequest sử dụng một proxy tor với ứng dụng asp.net của tôi và tôi nhận được thông báo lỗi này khi gọi các webresponse.GetResponse) phương pháp (:Máy chủ đã vi phạm giao thức. Section = ResponseStatusLine khi sử dụng một tor Proxy

The server committed a protocol violation. Section=ResponseStatusLine

Tôi đã thử tìm kiếm giải pháp trên web và tôi tìm thấy 3 giải pháp chính cho lỗi này:

  1. Thêm vào Web.config.

    <system.net> 
        <settings> 
        <httpWebRequest useUnsafeHeaderParsing="true"/> 
        </settings> 
    </system.net>` 
    
  2. Thêm dòng: webRequest.ProtocolVersion = HttpVersion.Version10; vào mã.

  3. Thêm dòng request.ServicePoint.Expect100Continue = false; vào mã.

Mỗi giải pháp được liệt kê không thay đổi điều gì về thông báo lỗi.

Dưới đây là đoạn code yêu cầu:

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; 

bất cứ ai có thể giúp tôi tìm một giải pháp cho việc này?

Trả lời

3

Bạn có thể biết thêm chi tiết về ngoại lệ bạn đang nhận được thực tế là WebException bằng cách xem tài sản Response của ngoại lệ và sau đó kiểm tra các thuộc tính StatusDescriptionStatusCode. Điều đó sẽ giúp bạn có thêm thông tin chi tiết về lỗi và hy vọng đưa bạn đến đúng hướng.

Something như thế này:

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

Ngoài ra, hãy nhìn vào WebException.Status dụ trên MSDN để biết thêm chi tiết

+3

Tôi đã thử thêm các dòng này nhưng trạng thái không phải là lỗi giao thức mà là ServerProtocolViolation. –

+3

Thật vậy, Trả lời là không cho ngoại lệ của tôi: ( –

+0

Cùng một vấn đề ở đây. Phản hồi là không. – tnktnk

0

tôi đã cùng một vấn đề. phương thức httpwebrequest getresponse luôn trả về lỗi giao thức lỗi và tôi đã giải quyết bằng cách này;

trước hết tôi sử dụng đối tượng xml com thay vì xdocument hoặc xmldocument.

đối tượng com này có một vài phiên bản Microsft XML, v3.0-v5.0-v6.0. tôi đã sử dụng phiên bản 7.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 
} 
Các vấn đề liên quan