From 7b29c06dba01ab4cdb62c866120307f477caf1e5 Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 2 Jun 2014 03:14:07 +0200 Subject: [PATCH] HttpClientsExamples: trying to check HTTP code --- .../HttpClientsExamples/HttpWebRequestExample.cs | 53 ++++++++++++---------- .../HttpClientsExamples/WebClientExample.cs | 21 ++++++++- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs index 9024410..3ce9321 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs @@ -28,12 +28,19 @@ namespace HttpClientsExamples HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(uri); try { using(HttpWebResponse httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse()) - using(Stream replyStream = httpWebResponse.GetResponseStream()) - using(StreamReader replyStreamReader = new StreamReader(replyStream)) { - Console.WriteLine(replyStreamReader.ReadToEnd()); - httpWebResponse.Dispose (); + // May httpWebResponse be null? API says nothing (as usual...) API sucks. + if (httpWebResponse.StatusCode == HttpStatusCode.OK) + { + using(Stream replyStream = httpWebResponse.GetResponseStream()) + using(StreamReader replyStreamReader = new StreamReader(replyStream)) + { + Console.WriteLine(replyStreamReader.ReadToEnd()); + httpWebResponse.Dispose (); + } + } } + } catch(ProtocolViolationException e) { Console.WriteLine ("Synchronous HttpWebRequest, ProtocolViolationException: ", e); @@ -68,33 +75,33 @@ namespace HttpClientsExamples return true; } else { Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e); - return false; + return true; } }); } if (task.Status == TaskStatus.RanToCompletion) { - try { - using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result) - using(Stream replyStream = httpWebResponse.GetResponseStream()) - using (StreamReader replyStreamReader = new StreamReader (replyStream)) + using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result) + { + // May httpWebResponse be null? API says nothing (as usual...) API sucks. + if (httpWebResponse.StatusCode == HttpStatusCode.OK) { - string s = replyStreamReader.ReadToEnd (); - Console.WriteLine (s); + 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); + } } } - catch(ProtocolViolationException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, ProtocolViolationException: ", e); - } - catch(NotSupportedException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, NotSupportedException: ", e); - } - catch(WebException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, WebException: ", e); - } - catch(IOException e) { - Console.WriteLine ("Asynchronous HttpWebRequest, IOException: ", e); - } } } } diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs index 97473be..a7d10ed 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs @@ -33,6 +33,12 @@ namespace HttpClientsExamples client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; Linux; Mono .NET 4.5)"); try { + // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!! + // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient + // When are we receiving WebException? When HTTP status code is not 200? I do not think so. + // What if I just want to read data when HTTP OK? I am getting really angry. + // C# is a great language but the API sucks IMHO. + // Be careful!!! If calling Dispose may throw exception, you could hide // exceptions being thrown inside the using blocks. In this case this Dispose methods // do not throw any exception but in other cases it could be different, so never forget it. @@ -175,12 +181,18 @@ namespace HttpClientsExamples return true; } else { Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e); - return false; + return true; } }); } if (task.Status == TaskStatus.RanToCompletion) { + // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!! + // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient + // When are we receiving WebException? When HTTP status code is not 200? I do not think so. + // What if I just want to read data when HTTP OK? I am getting really angry. + // C# is a great language but the API sucks IMHO. + try { using (Stream replyStream = task.Result) using (StreamReader replyStreamReader = new StreamReader (replyStream)) @@ -198,6 +210,13 @@ namespace HttpClientsExamples private void OpenReadCallback (Object sender, OpenReadCompletedEventArgs eventData) { + // HOW MAY I CHECK THE HTTP RESPONSE CODE? THIS SUCKS!!! + // From this answer: http://stackoverflow.com/questions/3574659/how-to-get-status-code-from-webclient + // When are we receiving WebException? When HTTP status code is not 200? I do not think so. + // What if I just want to read data when HTTP OK? I am getting really angry. + // The Mono implementation does not seem to throw WebException... so, the stack overflow answer must be wrong... FUUUUU + // C# is a great language but the API sucks IMHO. + var taskId = (Guid) eventData.UserState; if (eventData.Cancelled) -- 2.1.4