From: gu.martinm@gmail.com Date: Sun, 1 Jun 2014 23:07:28 +0000 (+0200) Subject: HttpClientsExamples: HttpWebRequest X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=e4efaedfeb30ba6805471535a02e75588c2cf81e;p=CSharpForFun%2F.git HttpClientsExamples: HttpWebRequest --- diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs index 6fb73be..9024410 100644 --- a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpWebRequestExample.cs @@ -1,12 +1,101 @@ using System; +using System.Net; +using System.IO; +using System.Threading.Tasks; +using System.Text; namespace HttpClientsExamples { + /** + * BE CAREFUL!!! If calling Dispose may throw exception, you could hide + * exceptions being thrown inside the using blocks. If the Dispose methods + * do not throw any exception there is no problem, + * otherwise see: http://msdn.microsoft.com/en-us/library/aa355056%28v=vs.110%29.aspx + */ + public class HttpWebRequestExample { public void Test() { + Console.WriteLine("Synchronous HttpWebRequest"); + 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(); + } + 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 (); + } + } + catch(ProtocolViolationException e) { + Console.WriteLine ("Synchronous HttpWebRequest, ProtocolViolationException: ", e); + } + catch(NotSupportedException e) { + Console.WriteLine ("Synchronous HttpWebRequest, NotSupportedException: ", e); + } + catch(WebException e) { + Console.WriteLine ("Synchronous HttpWebRequest, WebException: ", e); + } + catch(IOException e) { + Console.WriteLine ("Synchronous HttpWebRequest, IOException: ", e); + } + + Console.WriteLine("Asynchronous HttpWebRequest"); + 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(); + } + httpWebRequest = (HttpWebRequest) WebRequest.Create(uri); + Task task = httpWebRequest.GetResponseAsync (); + 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 false; + } + }); + } + if (task.Status == TaskStatus.RanToCompletion) + { + try { + using(HttpWebResponse httpWebResponse = (HttpWebResponse) task.Result) + 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(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); + } + } } } }