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
- Invoke the static create method for creating the HttpWebRequest such as.
- 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);
- Invoke the asynchronous BeginGetResponse method during invoke. Pass a HttpWebRequest object to access the asynchronous callback as shown in the following code.
- void GetRequestStream(IAsyncResult callbackResult)
- {
- HttpWebRequest sendRequest = (HttpWebRequest)callbackResult.AsyncState;
- Stream postStream = sendRequest.EndGetRequestStream(callbackResult);
- string JsonStringParams = "{\"user\":\"suresh\", \"password\":\"1234567\",\"email\":\"[email protected]\"} ";
- byte[] byteArray = Encoding.UTF8.GetBytes(JsonStringParams);
- postStream.Write(byteArray, 0, byteArray.Length);
- postStream.Flush();
- sendRequest.BeginGetResponse(new AsyncCallback(GetResponseStream), sendRequest);
- }
- Finally, get a response from the callback and then fetch the data stream to get the response.
- 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
- 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;
- }
- }
NoteWebClient and HttpWebRequest are invoked on a background thread, so use the Dispatcher to update the current UI.