Tôi cần đọc vị trí trong Bộ định tuyến của mình, nhưng tôi nhận được ngoại lệ sau - ServerProtocolViolation "Máy chủ đã vi phạm giao thức. Phần = ResponseHeader Chi tiết = CR phải được theo sau bởi LF"C#: Xử lý WebClient "vi phạm giao thức"
Điều này xảy ra khi tôi sử dụng hàm .DownloadString (url). Có cách nào để làm cho WebClient bỏ qua vi phạm giao thức? Các tìm kiếm trong Google cho tôi biết tôi nên đặt tùy chọn useUnsafeHeaderParsing ở đâu đó. Tôi có thể làm điều đó thông qua chương trình? Bắt nếu tôi sử dụng nó là gì?
Edit: Gắn mã -
public Readlog() {
WebClient wc = new WebClient();
string url = @"http://192.168.0.1/setup.cgi?next_file=log.htm&todo=cfg_init";
Console.WriteLine(url);
try {
//wc.Headers.Add("User-Agent", "Mozilla/5.0(Windows; U; Windows NT 5.2; rv:1.9.2) Gecko/20100101 Firefox/3.6");
wc.Credentials = new NetworkCredential("admin", "admin");
//Next line causes exception System.Net.WebException
//Message - "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF"
//May be I need to use useUnsafeHeaderParsing somehow to avoid that
string result = wc.DownloadString(url);
Console.WriteLine(result);
} catch (WebException we) {
System.Diagnostics.Trace.WriteLine(we.ToString());
}
}
Bạn có thể dán mã của mình vào câu hỏi. –