From: gu.martinm@gmail.com Date: Tue, 3 Jun 2014 19:07:31 +0000 (+0200) Subject: HttpClientsExamples: no time for comments X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=1a5c0de4659dbd83a2b8963aad4aedbc6ba8874e;p=CSharpForFun%2F.git HttpClientsExamples: no time for comments --- diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs index faf6a69..f1a6a84 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs @@ -172,8 +172,7 @@ namespace HttpClientsExamples } - // TODO: you must write another example with HttpCompletionOption.ResponseHeadersRead and I want - // to use in WebClientExamples and HttpWebRequestExample the same logic as in httpWebResponse.EnsureSuccessStatusCode + // TODO: you must write another example with HttpCompletionOption.ResponseHeadersRead } private async Task DoGetAsync(string uri) diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs index 3ce9321..1421679 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs @@ -30,17 +30,14 @@ namespace HttpClientsExamples using(HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse()) { // May httpWebResponse be null? API says nothing (as usual...) API sucks. - if (httpWebResponse.StatusCode == HttpStatusCode.OK) + this.EnsureSuccessStatusCode (httpWebResponse.StatusCode); + + using(Stream replyStream = httpWebResponse.GetResponseStream()) + using(StreamReader replyStreamReader = new StreamReader(replyStream)) { - using(Stream replyStream = httpWebResponse.GetResponseStream()) - using(StreamReader replyStreamReader = new StreamReader(replyStream)) - { - Console.WriteLine(replyStreamReader.ReadToEnd()); - httpWebResponse.Dispose (); - } + Console.WriteLine(replyStreamReader.ReadToEnd()); } } - } catch(ProtocolViolationException e) { Console.WriteLine ("Synchronous HttpWebRequest, ProtocolViolationException: ", e); @@ -84,26 +81,44 @@ namespace HttpClientsExamples using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result) { // May httpWebResponse be null? API says nothing (as usual...) API sucks. - if (httpWebResponse.StatusCode == HttpStatusCode.OK) - { - try { - using(Stream replyStream = httpWebResponse.GetResponseStream()) - using (StreamReader replyStreamReader = new StreamReader (replyStream)) - { - string s = replyStreamReader.ReadToEnd (); - Console.WriteLine (s); - } - } - catch(ProtocolViolationException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, ProtocolViolationException: ", e); - } - catch(IOException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, IOException: ", e); + this.EnsureSuccessStatusCode (httpWebResponse.StatusCode); + + try { + using(Stream replyStream = httpWebResponse.GetResponseStream()) + using (StreamReader replyStreamReader = new StreamReader (replyStream)) + { + string s = replyStreamReader.ReadToEnd (); + Console.WriteLine (s); } } + catch(ProtocolViolationException e) { + Console.WriteLine ("Asynchronous HttpWebRequest, ProtocolViolationException: ", e); + } + catch(IOException e) { + Console.WriteLine ("Asynchronous HttpWebRequest, IOException: ", e); + } } } } + + /** + * Taken from HttpResponseMessage.cs Mono implementation. + */ + private bool IsSuccessStatusCode(HttpStatusCode statusCode) { + // Successful codes are 2xx + return statusCode >= HttpStatusCode.OK && statusCode < HttpStatusCode.MultipleChoices; + } + + /** + * Taken from HttpResponseMessage.cs Mono implementation. + */ + private void EnsureSuccessStatusCode(HttpStatusCode statusCode) + { + if (this.IsSuccessStatusCode(statusCode)) + return; + + throw new Exception (string.Format ("{0}", (int) statusCode)); + } } }