Simple Async HTTP Request

by Manuel 7/24/2008 8:25:00 AM

Since .NET Framework 2.0 there is a very easy way to perform an asynchronous HTTP request and retrieve the result as a string.
The System.Net.WebClient class provides methods and events to handle this task in a very convenient way:


using System.Net; 

private void Button_Click(object sender, RoutedEventArgs e)
{
   string topic=this.SearchTextBox.Text;
   string url = "http://yourserver.com";
   WebClient client = new WebClient();
   client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
   client.DownloadStringAsync(
new Uri(url));
}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
   if (e.Error == null)
   {
      string result = e.Result;
   }
}

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

C# | ASP.NET

Related posts

Comments

2/27/2009 4:30:22 AM

Tabatha

Der Tat eine sehr einfache Art und Weise, so scheint es mir, es wäre verständlich, auch für Anfänger.

Tabatha us

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author Author name
Something about me and what I do.

E-mail me Send mail

Calendar

<<  August 2010  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in