HttpClientsExamples: HttpClient WIP
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 2 Jun 2014 01:35:16 +0000 (03:35 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 2 Jun 2014 01:35:16 +0000 (03:35 +0200)
Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs [new file with mode: 0644]

diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs
new file mode 100644 (file)
index 0000000..4ec49bc
--- /dev/null
@@ -0,0 +1,88 @@
+using System;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Net;
+using System.IO;
+
+namespace HttpClientsExamples
+{
+    public class HttpClientExample
+    {
+        public void Test()
+        {
+            Console.WriteLine("HttpClient");
+            string uri = null;
+            while (uri == null || uri.Length == 0)
+            {
+                Console.WriteLine("Specify the URI of the resource to retrieve.");
+                Console.WriteLine("Write URI: ");
+                uri = Console.ReadLine();
+            }
+            using (HttpClient client = new HttpClient { Timeout = TimeSpan.FromSeconds (5) })
+            {
+                Task<HttpResponseMessage> task =
+                    client.GetAsync (uri, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
+                try {
+                    Task.WaitAll (task);
+                } catch (AggregateException ae) {
+                    ae.Handle (e => {
+                        if (e is OperationCanceledException) {
+                            Console.WriteLine ("Cancelling a Task, catching OperationCanceledException: {0}", e);
+                            return true;
+                        } else {
+                            Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e);
+                            return true;
+                        }
+                    });
+                }
+
+                if (task.Status == TaskStatus.RanToCompletion)
+                {
+                    using(HttpResponseMessage httpWebResponse = task.Result)
+                    {
+                        // May httpWebResponse be null? API says nothing (as usual...) API sucks.
+                        if (httpWebResponse.StatusCode == HttpStatusCode.OK)
+                        {
+                            using(HttpContent content = httpWebResponse.Content)
+                            {
+                                // May content be null? API says nothing (as usual...) API sucks.
+                                Task<Stream> taskStream = content.ReadAsStreamAsync ();
+                                try {
+                                    Task.WaitAll (taskStream);
+                                } catch (AggregateException ae) {
+                                    ae.Handle (e => {
+                                        if (e is OperationCanceledException) {
+                                            Console.WriteLine ("Cancelling a Task, catching OperationCanceledException: {0}", e);
+                                            return true;
+                                        } else {
+                                            Console.WriteLine ("Cancelling a Task, dunno what are you: {0}", e);
+                                            return true;
+                                        }
+                                    });
+                                }
+
+                                if (taskStream.Status == TaskStatus.RanToCompletion)
+                                {
+                                    try {
+                                        using(Stream replyStream = taskStream.Result)
+                                        // May replyStream be null? API says nothing (as usual...) API sucks.
+                                        using (StreamReader replyStreamReader = new StreamReader (replyStream))
+                                        {
+                                            string s = replyStreamReader.ReadToEnd ();
+                                            Console.WriteLine (s);
+                                        }
+                                    }
+                                    catch(IOException e) {
+                                        Console.WriteLine ("HttpClient, IOException: ", e);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+}
+