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

Ähnliche Beiträge

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

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

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