2011-01-30 27 views
5

Her şeyi denedim ve bu hatanın neden oluştuğunu anlayamıyorum.Bizzare HttpWebResponse Hata: ServerProtocolViolation

Arka plan: Bir IPad uygulama, MonoTouch yazılmış olması ve arka planda çalışan bir iş parçacığı vardır ve her 15 saniyede bir sunucu ile veri senkronize edilir. Bu iş parçacığının ilk birkaç yineleme çalışır, ancak sonunda aşağıdaki yığın izini olsun.

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 

varsayılan yapılandırma ile bir IIS web sunucusu konuşuyorum.

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

yardım edin: İşte seni arıyorum yöntemdir. Ne yapacağımı bilmiyorum. Google hiçbir şey göstermiyor.

Hata gösterilmeden önce 22 başarılı istekte bulunabiliyorum.

DÜZENLEME Bir sunucu sorunu olmanın aşağı daralmış var. Bu asp.net MVC ve istisna sadece bir 304 müşteriye gönderdiğimde olur. Sunucu Kodu Bkz: istemci ve sunucu arasında bir proxy

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

Sunucu güzel davranmıyor demektir :) Ham yanıtın ne olduğunu görmek için Fiddler veya Ethereal gibi bir HTTP dinleyicisi kullanabilir misiniz? – Skurmedel

+0

22 kez çalışıp durur. Uygulamamı öldürürsem ve baştan başa başlasam ... her şey bir başka 22 istek için iyidir. Hala sunucuda olduğunu mu düşünüyorsun? –

+0

Yukarıdaki Düzenlemelere bakın. –

cevap

2
  • var mı?
  • 22 istek sonra her zaman başarısız olur mu? Özel durum, bazı UInt32'nin ayrıştırılamadığını gösterir.
  • Sunucu tarafında istisnalar var mı?
+0

Vekil yok, yaklaşık 22 istek ve sunucu istisnası yok. –

+0

İstemci kodunu gösterebilir misiniz? Yanlışlıkla müşteri tarafında bazı şeyleri yeniden kullanıyor olabilirsiniz. – msms

+0

Yukarıdaki Düzenlemelere bakın. –

1

Bu sorunun cevabını arayan başka bir kişi interneti araştırıyorsa, yazdığım bazı işlevsel testlerle çok benzer bir problem yaşadım. Http PUT kullanarak bir web sunucusuna içerik yüklüyorlar ve daha sonra düz bir şekilde http GET kullanıyorlardı.

GET, Uint32 ayrıştırmasında aynı hatayla güvenilir bir şekilde başarısız oldu. Windows yapı iyiydi. Çaresizlik içinde http istekleri arasında biraz gecikme eklemek için Thread.Sleep (20) kullanıyorum ve bu hatayı giderdi. Neden olduğundan emin değilsin, ama şimdi çalışıyor, bu da benim üretim için değil, testlerim için yeterince iyi.