2011-01-30 4 views
5

Ho provato tutto e non riesco a capire perché questo errore si verifica.Errore Bray HttpWebResponse: ServerProtocolViolation

Sfondo: Ho un'applicazione IPad, scritta in MonoTouch e ho un filo che viene eseguito in background, e ogni 15 secondi ho sincronizzare i dati con il server. Funziona le prime iterazioni del thread, ma alla fine ottengo la seguente traccia dello stack.

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 

sto parlando a un server web IIS con la configurazione di default. Ecco il metodo che io chiamo:

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

prega di aiuto. Io non so cosa fare. Google non mostra nulla.

Sono in grado di effettuare 22 richieste riuscite prima che si verifichi l'errore.

EDIT ho ristretto la scelta ad essere un problema di server. Questo è asp.net MVC e l'eccezione si verifica solo quando invio un 304 al client. Vedere codice del server:

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

Significa che il server non funziona bene :) È possibile utilizzare uno sniffer HTTP come Fiddler o Ethereal per vedere qual è la risposta non elaborata? – Skurmedel

+0

Funziona 22 volte, poi si ferma. Se uccido la mia app e ricomincia da capo ... tutto va bene per altre 22 richieste. Pensi ancora che sia sul server? –

+0

Vedere le modifiche sopra. –

risposta

2
  • C'è un proxy tra il client e il server ?
  • Fallisce sempre dopo 22 richieste? L'eccezione indica che alcuni UInt32 non possono essere analizzati.
  • Ricevi eccezioni sul lato server?
+0

Nessun proxy, circa 22 richieste e nessuna eccezione del server. –

+0

Puoi mostrare il codice cliente? Potresti erroneamente riutilizzare qualcosa dal lato client. – msms

+0

Vedere le modifiche sopra. –

1

Nel caso in cui qualcun altro stia sondando Internet cercando risposte su questo problema, ho avuto un problema molto simile con alcuni test funzionali che stavo scrivendo. Stavano caricando contenuti su un server web usando http PUT e poi usando http GET subito dopo.

Il GET non è riuscito in modo affidabile con lo stesso errore nell'analisi Uint32. La costruzione di finestre andava bene. In preda alla disperazione ho usato Thread.Sleep (20) per inserire un ritardo tra le richieste http e questo ha chiarito l'errore. Non so perché, ma funziona ora, il che è abbastanza buono per i miei test, se non per roba di produzione.