HttpClientsExamples: working on HttpWebRequestExample
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 29 May 2014 00:43:58 +0000 (02:43 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 29 May 2014 00:43:58 +0000 (02:43 +0200)
Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.MonoDevelop.csproj
Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs [new file with mode: 0644]
Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs
Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs

index d5fced8..025db8e 100644 (file)
@@ -38,6 +38,7 @@
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="WebClientExample.cs" />
+    <Compile Include="HttpWebRequestExample.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>
\ No newline at end of file
diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs
new file mode 100644 (file)
index 0000000..6fb73be
--- /dev/null
@@ -0,0 +1,13 @@
+using System;
+
+namespace HttpClientsExamples
+{
+    public class HttpWebRequestExample
+    {
+        public void Test()
+        {
+
+        }
+    }
+}
+
index ae51cea..c9ba93e 100644 (file)
@@ -8,6 +8,8 @@ namespace HttpClientsExamples
         {
             WebClientExample webclientExample = new WebClientExample();
             webclientExample.Test();
+            HttpWebRequestExample httpWebRequestExample = new HttpWebRequestExample();
+            httpWebRequestExample.Test();
         }
     }
 }
index 641fa48..b2a1e8f 100644 (file)
@@ -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<Stream> 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);
+                    }
                 }
             }
         }