Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Consume REST Service Using HttpWebRequest and WebClient in Windows Phone 8 & 8.1

DZone's Guide to

Consume REST Service Using HttpWebRequest and WebClient in Windows Phone 8 & 8.1

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

In this  article, we will see how to consume a REST service in Windows Phone 8 and 8.1. Before that we will introduce REST services.

REST is a resource that implements a uniform interface using standard HTTP GET, POST, PUT methods that can be located by URI.

Windows Phone supports basic get and post web requests using the following two classes:

  • HttpWebRequest: less flexible.
  • WebClient: portable and simple.

HttpWebRequest is more useful because we can specify headers, content type and more control over the request. 

Procedure

  1. Invoke the static create method for creating the HttpWebRequest such as.
    1. string AuthServiceUri = "http://112.165.1.31:8070/userRegistration"; 
      HttpWebRequest httpRequest = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest; 
      httpRequest.ContentType = "application/json"; 
      httpRequest.Method = "POST"; 
      httpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStream), httpRequest);  
  2. Invoke the asynchronous BeginGetResponse method during invoke. Pass a HttpWebRequest object to access the asynchronous callback as shown in the following code.
    1. void GetRequestStream(IAsyncResult callbackResult) 
      { 
      HttpWebRequest sendRequest = (HttpWebRequest)callbackResult.AsyncState; 
      Stream postStream = sendRequest.EndGetRequestStream(callbackResult); 
      string JsonStringParams = "{\"user\":\"suresh\", \"password\":\"1234567\",\"email\":\"sureshsri.sri0@gmail.com\"} "; 
      byte[] byteArray = Encoding.UTF8.GetBytes(JsonStringParams); 
      postStream.Write(byteArray, 0, byteArray.Length); 
      postStream.Flush(); 
      sendRequest.BeginGetResponse(new AsyncCallback(GetResponseStream), sendRequest); 
      }  
  3. Finally, get a response from the callback and then fetch the data stream to get the response.
    1. void GetResponseStream(IAsyncResult callbackResult) 
      { 
       try
       { 
       HttpWebRequest request = (HttpWebRequest) callbackResult.AsyncState; 
       HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(callbackResult); 
       string responseString = string.Empty; 
       Stream streamResponse = response.GetResponseStream(); 
       StreamReader reader = new StreamReader(streamResponse); 
       responseString = reader.ReadToEnd(); 
       streamResponse.Flush(); 
       string result = responseString; 
       } 
       catch (Exception e) 
       { 
       throw e; 
       } 
      }  

The WebClient class is a Base Class Library (BCL) for downloading and uploading web content.

First, create a webclient instance and call the DownloadStringCompleted or OpenReadCompleted event handler to process the result.

C# Code 

  1. void GetResponseStream(IAsyncResult callbackResult) 
    { 
     try
     { 
     HttpWebRequest request = (HttpWebRequest) callbackResult.AsyncState; 
     HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(callbackResult); 
     string responseString = string.Empty; 
     Stream streamResponse = response.GetResponseStream(); 
     StreamReader reader = new StreamReader(streamResponse); 
     responseString = reader.ReadToEnd(); 
     streamResponse.Flush(); 
     string result = responseString; 
     } 
     catch (Exception e) 
     { 
     throw e; 
     } 
    }  

Note

WebClient and HttpWebRequest are invoked on a background thread, so use the Dispatcher to update the current UI. 

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
opinion ,mobile ,web services ,rest ,rest services ,windows 8 ,windows phone ,windows phone 8

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}