From 7208447b3f996c59589e7d020d3aa2d0004da0af Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 2 Jun 2014 03:35:16 +0200 Subject: [PATCH] HttpClientsExamples: HttpClient WIP --- .../HttpClientsExamples/HttpClientExample.cs | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs new file mode 100644 index 0000000..4ec49bc --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientExample.cs @@ -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 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 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); + } + } + } + } + } + } + } + } + } +} + -- 2.1.4