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

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";  
    2. HttpWebRequest httpRequest = HttpWebRequest.Create(AuthServiceUri) as HttpWebRequest;  
    3. httpRequest.ContentType = "application/json";  
    4. httpRequest.Method = "POST";  
    5. 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)  
    2. {  
    3. HttpWebRequest sendRequest = (HttpWebRequest)callbackResult.AsyncState;  
    4. Stream postStream = sendRequest.EndGetRequestStream(callbackResult);  
    5. string JsonStringParams = "{\"user\":\"suresh\", \"password\":\"1234567\",\"email\":\"[email protected]\"} ";  
    6. byte[] byteArray = Encoding.UTF8.GetBytes(JsonStringParams);  
    7. postStream.Write(byteArray, 0, byteArray.Length);  
    8. postStream.Flush();  
    9. sendRequest.BeginGetResponse(new AsyncCallback(GetResponseStream), sendRequest);  
    10. }  
  3. Finally, get a response from the callback and then fetch the data stream to get the response.
    1. void GetResponseStream(IAsyncResult callbackResult)  
    2. {  
    3.     try  
    4.     {  
    5.         HttpWebRequest request = (HttpWebRequest) callbackResult.AsyncState;  
    6.         HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(callbackResult);  
    7.         string responseString = string.Empty;  
    8.         Stream streamResponse = response.GetResponseStream();  
    9.         StreamReader reader = new StreamReader(streamResponse);  
    10.         responseString = reader.ReadToEnd();  
    11.         streamResponse.Flush();  
    12.         string result = responseString;  
    13.     }  
    14.     catch (Exception e)  
    15.     {  
    16.         throw e;  
    17.     }  
    18. }  

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)   
  2. {  
  3.     try   
  4.     {  
  5.         HttpWebRequest request = (HttpWebRequest) callbackResult.AsyncState;  
  6.         HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(callbackResult);  
  7.         string responseString = string.Empty;  
  8.         Stream streamResponse = response.GetResponseStream();  
  9.         StreamReader reader = new StreamReader(streamResponse);  
  10.         responseString = reader.ReadToEnd();  
  11.         streamResponse.Flush();  
  12.         string result = responseString;  
  13.     }   
  14.     catch (Exception e)  
  15.     {  
  16.         throw e;  
  17.     }  
  18. }  
Note

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


Similar Articles