From 7ca66e92d12ff02dc2277b55676f3d4840d060ab Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 26 May 2014 17:12:16 +0200 Subject: [PATCH] .NET HTTP client implementations: examples. Trying out the .NET HTTP client implementations that I know (ATM) --- .../HttpClientsExamples/HttpClientsExamples.sln | 33 ++++++ .../HttpClientsExamples/HttpClientsExamples.csproj | 43 +++++++ .../HttpClientsExamples/Program.cs | 13 ++ .../HttpClientsExamples/Properties/AssemblyInfo.cs | 22 ++++ .../HttpClientsExamples/WebClientExample.cs | 132 +++++++++++++++++++++ 5 files changed, 243 insertions(+) create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples.sln create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.csproj create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples/Properties/AssemblyInfo.cs create mode 100644 Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples.sln b/Allgemeines/HttpClientsExamples/HttpClientsExamples.sln new file mode 100644 index 0000000..c26afbf --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples.sln @@ -0,0 +1,33 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientsExamples", "HttpClientsExamples\HttpClientsExamples.csproj", "{67B3556B-3F66-4209-AEFE-B88C43437475}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {67B3556B-3F66-4209-AEFE-B88C43437475}.Debug|x86.ActiveCfg = Debug|x86 + {67B3556B-3F66-4209-AEFE-B88C43437475}.Debug|x86.Build.0 = Debug|x86 + {67B3556B-3F66-4209-AEFE-B88C43437475}.Release|x86.ActiveCfg = Release|x86 + {67B3556B-3F66-4209-AEFE-B88C43437475}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = HttpClientsExamples\HttpClientsExamples.csproj + Policies = $0 + $0.TextStylePolicy = $1 + $1.FileWidth = 120 + $1.inheritsSet = VisualStudio + $1.inheritsScope = text/plain + $1.scope = text/x-csharp + $0.CSharpFormattingPolicy = $2 + $2.BeforeMethodDeclarationParentheses = False + $2.BeforeConstructorDeclarationParentheses = False + $2.AfterDelegateDeclarationParameterComma = True + $2.inheritsSet = Mono + $2.inheritsScope = text/x-csharp + $2.scope = text/x-csharp + EndGlobalSection +EndGlobal diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.csproj b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.csproj new file mode 100644 index 0000000..d5fced8 --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/HttpClientsExamples.csproj @@ -0,0 +1,43 @@ + + + + Debug + x86 + 10.0.0 + 2.0 + {67B3556B-3F66-4209-AEFE-B88C43437475} + Exe + HttpClientsExamples + HttpClientsExamples + v4.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + true + x86 + + + full + true + bin\Release + prompt + 4 + true + x86 + + + + + + + + + + + \ No newline at end of file diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs new file mode 100644 index 0000000..ae51cea --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Program.cs @@ -0,0 +1,13 @@ +using System; + +namespace HttpClientsExamples +{ + class MainClass + { + public static void Main(string[] args) + { + WebClientExample webclientExample = new WebClientExample(); + webclientExample.Test(); + } + } +} diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/Properties/AssemblyInfo.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..c65491f --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/Properties/AssemblyInfo.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. +[assembly: AssemblyTitle ("HttpClientsExamples")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("gmm003es")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. +[assembly: AssemblyVersion ("1.0.*")] +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs new file mode 100644 index 0000000..caa39b8 --- /dev/null +++ b/Allgemeines/HttpClientsExamples/HttpClientsExamples/WebClientExample.cs @@ -0,0 +1,132 @@ +using System; +using System.Net; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace HttpClientsExamples +{ + public class WebClientExample + { + public void Test() + { + Console.WriteLine("Synchronous WebClient"); + string line = null; + while (line == null || line.Length == 0) + { + Console.WriteLine("Specify the URI of the resource to retrieve."); + Console.WriteLine("Write URI: "); + line = Console.ReadLine(); + } + + using (WebClient client = new WebClient()) + { + // Add a user agent header in case the + // requested URI contains a query. + client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; Linux; Mono .NET 4.5)"); + + using (Stream data = client.OpenRead (line)) + using (StreamReader reader = new StreamReader(data)) + { + string s = reader.ReadToEnd(); + Console.WriteLine(s); + } + // data.Dispose(); + // data.Close(); + // reader.Dispose(); + // reader.Close(); + } + // client.Dispose(); + + + line = null; + Console.WriteLine("Asynchronous WebClient With Events"); + while (line == null || line.Length == 0) + { + Console.WriteLine("Specify the URI of the resource to retrieve."); + Console.WriteLine("Write URI: "); + line = Console.ReadLine(); + } + + using (WebClient client = new WebClient()) + { + // Another way, using DownloadDataAsync and its delegate. + // client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadDataCallback); + // client.DownloadDataAsync (uri, waiter); + client.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback); + client.OpenReadAsync(new Uri(line), null /*What is this for? :( */); + } + // How to wait for client end without using sleep? :( + Thread.Sleep(3000); + + + line = null; + Console.WriteLine("Asynchronous WebClient With Tasks"); + while (line == null || line.Length == 0) + { + Console.WriteLine("Specify the URI of the resource to retrieve."); + Console.WriteLine("Write URI: "); + line = Console.ReadLine(); + } + using (WebClient client = new WebClient()) + using (Task data = client.OpenReadTaskAsync(line)) + //using (Stream stream = data.Result) + { + data.Start(); + try { + Task.WaitAll(data); + } + catch (AggregateException ae) + { + ae.Handle(e => + { + if (e is OperationCanceledException) + { + Console.WriteLine("Cancelling a Task, catching OperationCanceledException: {0} {1}", e.Message, e.StackTrace); + return true; + } + else + { + Console.WriteLine("Cancelling a Task, dunno what are you: {0} {1}", e.Message, e.StackTrace); + return false; + } + }); + } + Stream dataStream = data.Result; + if (dataStream != null) + { + + } + } + } + + private void OpenReadCallback (Object sender, OpenReadCompletedEventArgs e) + { + Stream reply = null; + StreamReader s = null; + + // Old fashined way (without using) + // NOTE: using statement is the same as this code (it also checks for null value before calling + // the Dispose method) + try + { + reply = (Stream)e.Result; + s = new StreamReader (reply); + Console.WriteLine (s.ReadToEnd ()); + } + finally + { + if (s != null) + { + s.Close (); + } + + if (reply != null) + { + reply.Close (); + } + } + } + } +} + -- 2.1.4