2011-01-30 18 views
5

Tôi đã thử tất cả mọi thứ và tôi không thể tìm ra lý do tại sao lỗi này xảy ra.Bizzare HttpWebResponse Lỗi: ServerProtocolViolation

Bối cảnh: Tôi có một ứng dụng IPad, được viết bằng MonoTouch và tôi có một luồng chạy ngầm và cứ sau 15 giây tôi đồng bộ dữ liệu với máy chủ. Điều này làm việc một vài lần lặp đầu tiên của luồng, nhưng cuối cùng tôi nhận được dấu vết ngăn xếp sau đây.

An exception occured: System.Net.WebException: Error getting response stream (ReadDone4): ServerProtocolViolation ---> System.FormatException: Input string was not in the correct format 
    at System.UInt32.Parse (System.String s) [0x00010] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System/UInt32.cs:405 
    at System.Net.WebConnection.GetResponse (System.Byte[] buffer, Int32 max) [0x000ba] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:565 
    at System.Net.WebConnection.ReadDone (IAsyncResult result) [0x00095] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnection.cs:446 
    --- End of inner exception stack trace --- 
    at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) [0x0005e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:819 
    at System.Net.HttpWebRequest.GetResponse() [0x0000e] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:827 
    at SyncService.REST.RestClient.Execute[IEnumerable`1] (SyncService.REST.RestRequest request) [0x00079] in /Users/Chris/Compass/SyncService/REST/RestClient.cs:42 

Tôi đang nói chuyện với một IIS webserver với cấu hình mặc định. Đây là phương pháp tôi đang gọi:

public RestResponse<T> Execute<T>(RestRequest request){ 
    var restResponse = new RestResponse<T>(); 
    var serializer = new JavaScriptSerializer(); 
    var urlPath = _baseUrl + "/" + request.Resource; 
    var httpRequest = (HttpWebRequest)HttpWebRequest.Create(new Uri(urlPath)); 

    httpRequest.Headers = request.Headers; 
    Authenticator.Authenticate(httpRequest); 

    httpRequest.Method = request.Method.ToString(); 
    if (request.Method == Method.POST) 
     SetPostData(httpRequest, request); 

    HttpWebResponse httpResponse = null; 
    try{ 
     httpResponse = (HttpWebResponse) httpRequest.GetResponse(); 
     var reader = new StreamReader(httpResponse.GetResponseStream()); 
     var responseString = reader.ReadToEnd(); 
     reader.Close(); 
     restResponse.StatusCode = httpResponse.StatusCode; 
     restResponse.Headers = httpResponse.Headers; 
     restResponse.Data = serializer.Deserialize<T>(responseString); 
     restResponse.ResponseStatus = ResponseStatus.Completed; 
    } 
    catch(WebException e){ 

     restResponse.ResponseStatus = ResponseStatus.Error; 
     restResponse.ErrorMessage = e.Message; 
     restResponse.ErrorException = e; 
     var webResponse = (HttpWebResponse) e.Response; 
     if (webResponse != null){ 
      restResponse.StatusCode = webResponse.StatusCode; 
      restResponse.Headers = webResponse.Headers; 
     } 
     if (restResponse.StatusCode != HttpStatusCode.NotModified) 
      Console.WriteLine("An exception occured: " + e + "\r\n"); 
    }catch (Exception ex) { 
     restResponse.ResponseStatus = ResponseStatus.Error; 
     restResponse.ErrorMessage = ex.Message; 
     restResponse.ErrorException = ex; 
    } 

    if (httpResponse != null) 
     httpResponse.Close(); 

    return restResponse; 
} 

Xin vui lòng trợ giúp. Tôi không biết phải làm gì. Google không hiển thị gì.

Tôi có thể thực hiện 22 yêu cầu thành công trước khi lỗi xuất hiện.

EDIT Tôi đã thu hẹp nó xuống dưới dạng sự cố máy chủ. Đây là asp.net MVC và ngoại lệ chỉ xảy ra khi tôi gửi 304 cho khách hàng. Xem server Code:

private void ServeHttpStatusCode() { 
    Response.StatusCode = 304; 
    Response.Status = "304 Not Modified"; 
    Response.StatusDescription = "The resource you are requesting has not been modified"; 
    Response.ContentType = "application/json"; 
    Response.Write("{\"Error\":\"The resource you are requesting has not been modified\"}"); 
    Response.End(); 
    Response.Close(); 
} 
+0

Điều đó có nghĩa là máy chủ không hoạt động độc đáo :) Bạn có thể sử dụng trình thám thính HTTP như Fiddler hoặc Ethereal để xem phản hồi thô không? – Skurmedel

+0

Làm việc 22 lần rồi dừng lại. Nếu tôi giết ứng dụng của tôi và bắt đầu lại ... tất cả đều tốt cho 22 yêu cầu khác. Bạn vẫn nghĩ rằng nó trên máy chủ? –

+0

Xem Chỉnh sửa ở trên. –

Trả lời

2
  • Có một proxy giữa máy khách và máy chủ ?
  • Không phải lúc nào cũng thất bại sau 22 yêu cầu? Ngoại lệ cho biết một số UInt32 không thể được phân tích cú pháp.
  • Bạn có ngoại lệ ở phía máy chủ không?
+0

Không có proxy, khoảng 22 yêu cầu và không có ngoại lệ máy chủ. –

+0

Bạn có thể hiển thị mã máy khách không? Bạn có thể nhầm lẫn đang sử dụng lại một số thứ ở phía khách hàng. – msms

+0

Xem Chỉnh sửa ở trên. –

1

Trong trường hợp bất kỳ ai khác đang tìm kiếm câu trả lời về vấn đề này, tôi gặp phải một vấn đề rất giống với một số bài kiểm tra chức năng tôi đã viết. Họ đang tải nội dung lên máy chủ web bằng cách sử dụng http PUT và sau đó sử dụng http GET ngay sau đó.

Lỗi GET không thành công với cùng một lỗi trong phân tích cú pháp Uint32. Các cửa sổ xây dựng là tốt. Trong tuyệt vọng tôi sử dụng Thread.Sleep (20) để chèn một số chậm trễ giữa các yêu cầu http, và điều này đã xóa lỗi. Không chắc chắn lý do tại sao, nhưng nó hoạt động ngay bây giờ, đó là đủ tốt cho các bài kiểm tra của tôi, nếu không cho công cụ sản xuất.

Các vấn đề liên quan