HttpClientsExamples: no time for comments
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 3 Jun 2014 19:07:31 +0000 (21:07 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Tue, 3 Jun 2014 19:07:31 +0000 (21:07 +0200)
Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs
Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs

index faf6a69..f1a6a84 100644 (file)
@@ -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<string> DoGetAsync(string uri)
index 3ce9321..1421679 100644 (file)
@@ -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));
+        }
     }
 }