Making a HTTP request and listening its completion in Windows Phone
(Mandardac - This article include how to make http request using WebClient and listening requst completion using EventHandler.) |
m (Croozeus - Fixed abstract) |
||
| Line 1: | Line 1: | ||
[[Category:Draft]][[Category:Windows Phone]] | [[Category:Draft]][[Category:Windows Phone]] | ||
| − | {{Abstract| | + | {{Abstract|This article include how to make http request using WebClient and listening requst completion using EventHandler.}} |
| − | |||
{{ArticleMetaData <!-- v1.2 --> | {{ArticleMetaData <!-- v1.2 --> | ||
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | |sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] --> | ||
Revision as of 09:01, 2 May 2012
This article include how to make http request using WebClient and listening requst completion using EventHandler.
Article Metadata
Introduction
HTTPManeger.cs
public class HTTPManager
{
private WebClient webdownloader;
private Uri uri;
public event EventHandler WebRequestCompleted;
public HTTPManager(string url)
{
webdownloader = new WebClient();
uri = new Uri(url);
}
public void MakeRequest()
{
webdownloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DataDownloaded);
webdownloader.DownloadStringAsync(uri);
}
public void DataDownloaded(object sender, DownloadStringCompletedEventArgs args)
{
EventHandler handler = WebRequestCompleted;
if (handler != null)
{
handler(sender , args);
}
}
}
//now write another class which actully uses above class to make http request and wants to listen request completion
public partial class MainPage : PhoneApplicationPage
{
private string loginurl;
// Constructor
public MainPage()
{
InitializeComponent();
MakeRequest();
}
private void MakeRequest() //you can call this method from any event eg.button click, for simplicity i called it from ctor.
{
string loginurl = "http://www.developer.nokia.com/Community/Wiki/Most_Viewed_Windows_Phone_Articles";
try
{
if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
MessageBox.Show("N/W connection not available");
}
else
{
HTTPManager httpmanager = new HTTPManager(loginurl);
httpmanager.WebRequestCompleted += this.DownlLodCompleted;
httpmanager.MakeRequest();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void DownlLodCompleted(object sender, EventArgs args)
{
MessageBox.Show("Your request is completed.....");
}
}

