From 1a5c0de4659dbd83a2b8963aad4aedbc6ba8874e Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Tue, 3 Jun 2014 21:07:31 +0200 Subject: [PATCH] HttpClientsExamples: no time for comments --- .../HttpClientsExamples/HttpClientExample.cs | 3 +- .../HttpClientsExamples/HttpWebRequestExample.cs | 61 ++++++++++++++-------- 2 files changed, 39 insertions(+), 25 deletions(-) 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)); + } } } -- 2.1.4