×
Namespaces

Variants
Actions
(Difference between revisions)

Making a HTTP request and listening its completion in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Addition to article of: Category:Windows Phone 7.5)
chintandave_er (Talk | contribs)
(Chintandave er - added WP8 category)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Networking]][[Category:HTTP]]
+
[[Category:Windows Phone]][[Category:Networking]][[Category:HTTP]][[Category:Windows Phone 7.5]][[Category:Windows Phone 8]]
 
{{Abstract|This article include how to make HTTP request using {{Icode|WebClient}} and listening requst completion using EventHandler.}}  
 
{{Abstract|This article include how to make HTTP request using {{Icode|WebClient}} and listening requst completion using EventHandler.}}  
  
Line 7: Line 7:
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|sdk= WP 7.1 SDK
 
|sdk= WP 7.1 SDK
|platform= Windows Phone 7.5
+
|platform= Windows Phone 7.5, 8
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
Line 94: Line 94:
 
       }
 
       }
 
   }
 
   }
</code>[[Category:Windows Phone 7.5]]
+
</code>

Revision as of 20:00, 16 December 2012

This article include how to make HTTP request using WebClient and listening requst completion using EventHandler.

WP Metro Icon Wifi.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
SDK: WP 7.1 SDK
Compatibility
Platform(s): Windows Phone 7.5, 8
Windows Phone 8
Windows Phone 7.5
Article
Created: mandardac (04 Apr 2012)
Last edited: chintandave_er (16 Dec 2012)

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.....");
}
}
667 page views in the last 30 days.
×