From: gu.martinm@gmail.com Date: Thu, 29 May 2014 00:43:58 +0000 (+0200) Subject: HttpClientsExamples: working on HttpWebRequestExample X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=4907a59d9b420bc58ff4cc7aa7100d378f435838;p=CSharpForFun%2F.git HttpClientsExamples: working on HttpWebRequestExample --- diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.MonoDevelop.csproj b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.MonoDevelop.csproj index d5fced8..025db8e 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.MonoDevelop.csproj +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.MonoDevelop.csproj @@ -38,6 +38,7 @@ + \ No newline at end of file diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs new file mode 100644 index 0000000..6fb73be --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs @@ -0,0 +1,13 @@ +using System; + +namespace HttpClientsExamples +{ + public class HttpWebRequestExample + { + public void Test() + { + + } + } +} + diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs index ae51cea..c9ba93e 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs @@ -8,6 +8,8 @@ namespace HttpClientsExamples { WebClientExample webclientExample = new WebClientExample(); webclientExample.Test(); + HttpWebRequestExample httpWebRequestExample = new HttpWebRequestExample(); + httpWebRequestExample.Test(); } } } diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs index 641fa48..b2a1e8f 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs @@ -130,7 +130,8 @@ namespace HttpClientsExamples * DO NOT BOTHER DISPOSING OF YOUR TASKS: http://blogs.msdn.com/b/pfxteam/archive/2012/03/25/10287435.aspx */ Task task = client.OpenReadTaskAsync (line); - task.Start (); + // Don't do this. OpenReadTaskAsync is already launching a new Thread (OpenReadTaskAsync is intended to be used with async/await) + //task.Start (); try { Task.WaitAll (task); } catch (AggregateException ae) { @@ -144,12 +145,15 @@ namespace HttpClientsExamples } }); } - // I am starting to love the using statement instead of traditional try/finally block with check for null values and close. - using (Stream replyStream = task.Result) - using (StreamReader replyStreamReader = new StreamReader (replyStream)) + if (task.Status == TaskStatus.RanToCompletion) { - string s = replyStreamReader.ReadToEnd (); - Console.WriteLine (s); + // I am starting to love the using statement instead of traditional try/finally block with check for null values and close. + using (Stream replyStream = task.Result) + using (StreamReader replyStreamReader = new StreamReader (replyStream)) + { + string s = replyStreamReader.ReadToEnd (); + Console.WriteLine (s); + } } } }