×
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
croozeus (Talk | contribs)
m (Croozeus - - Introduction: Added code tags)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and links)
Line 9: Line 9:
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|platform= <!-- Compatible platforms - e.g. Symbian^1 and later, Qt 4.6 and later -->
 
|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 -->
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
+
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-by= <!-- After significant update: [[User:username]]-->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
|update-timestamp= <!-- After significant update: YYYYMMDD -->
|creationdate= <!-- Format YYYYMMDD -->
+
|creationdate= 20120404
|author= <!-- Display as link [[User:username]] -->
+
|author= [[User:Mandardac]]
 
}}
 
}}
  
Line 34: Line 34:
 
         private Uri uri;
 
         private Uri uri;
 
         public event EventHandler WebRequestCompleted;
 
         public event EventHandler WebRequestCompleted;
         public HTTPManager(string url)
+
         public HTTPManager(string url)
 
         {
 
         {
 
             webdownloader = new WebClient();
 
             webdownloader = new WebClient();
Line 49: Line 49:
 
             if (handler != null)
 
             if (handler != null)
 
             {
 
             {
                 handler(sender , args);
+
                 handler(sender, args);
 
             }
 
             }
 
         }
 
         }
Line 70: Line 70:
 
         private void MakeRequest() //you can call this method from any event eg.button click,  for simplicity i called it from ctor.
 
         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";
+
               string loginurl = "http://www.developer.nokia.com/Community/Wiki/Most_Viewed_Windows_Phone_Articles";
 
               try
 
               try
 
                 {
 
                 {
Line 91: Line 91:
 
       public void DownlLodCompleted(object sender, EventArgs args)
 
       public void DownlLodCompleted(object sender, EventArgs args)
 
       {
 
       {
             MessageBox.Show("Your request is completed.....");
+
             MessageBox.Show("Your request is completed.....");
 
       }
 
       }
 
   }
 
   }
 
</code>
 
</code>

Revision as of 03:14, 11 July 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
CompatibilityArticle
Created: mandardac (04 Apr 2012)
Last edited: hamishwillee (11 Jul 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.....");
}
}
627 page views in the last 30 days.
×