C# HTTP Request

A few years ago I've written an article on how to perform a synchronous HTTP request using Qt 4.2. I I've seen this article today and since now I'm mostly working with C# i was wandering how fast can i do the same thing in c#. After very few minutes I've managed to came up with this:

 1 using System;
 2 using System.Net;
 3 using System.IO;
 4 
 5 class Program
 6 {
 7     static void Main(string[] args)
 8     {
 9         WebRequest request = HttpWebRequest.Create("http://www.google.com");
10         WebResponse response = request.GetResponse();
11         using(StreamReader reader = new StreamReader(response.GetResponseStream()))
12             Console.WriteLine(reader.ReadToEnd());
13         Console.ReadKey();
14     }
15 }

And of course the WebReqest class allows you to specify all the properties you would ever need for any request, and also allows you to perform request in an asynchronous maner. Now i don't think you can be more expressive than this. I like qt a lot... but i love c# now.

Update...

Or you can do this:

 1 using System;
 2 using System.Net;
 3 using System.IO;
 4 
 5 class Program
 6 {
 7     static void Main(string[] args)
 8     {
 9         var content = new WebClient().DownloadString("http://www.google.com");
10         Console.WriteLine(content);
11     }
12 }

Comments