HTTP-based services return data when an HTTP request is sent to a particular URI.
HTTP requests can be sent to a variety of URIs. Examples of such URIs include http://fabrikam.com/service/getUser, http://fabrikam.com/service.svc/getUser, or even static data such as http://fabrikam.com/data.xml. The HTTP request is usually configured with the GET verb, which implies retrieval, or the POST verb, which implies invocation. The service being accessed might require parameters to be sent with the request. For HTTP GET, parameters are usually appended to the end of the URI: http://fabrikam.com/service/getUser?id=123. For HTTP POST, any parameters are part of the body of the HTTP request.
The data the service returns is contained in the HTTP response. The format of the data depends on the service implementation. Common data formats returned by HTTP-based services include XML, JSON, or RSS/Atom. The format of the data and its schema (except in the case of RSS/Atom, which have standard schemas) are usually described by the developer of the service in human-readable documentation.
Silverlight provides two methods for sending HTTP requests: WebClient and >HttpWebReqest. There are some important difference between these two methods:
- HttpWebReqest supports a larger subset of the HTTP protocol, which tends to make it more appropriate for some advanced scenarios.
- HttpWebReqest uses an asynchronous programming model using delegates, whereas WebClient uses an event-based asynchronous programming model. The event-based model is simpler to use and generally requires fewer lines of code.
- The WebClient callback, which is raised when the HTTP response is returned, is invoked on the User Interface (UI) thread, and can be used to update the properties of UI elements. It can, for example, be used to display the data in the HTTP response. The HttpWebReqest callback, by comparison, is not returned on the UI thread, so extra code is needed to work with the UI from that callback. This tends to make WebClient a better fit for applications that need to update UI.
These differences make WebClient easier choice to use for the purposes of this topic.
To call an HTTP-based service
-
Create the URI to which the request is sent. This URI can point to static data, for example an RSS/Atom feed stored in a file /data.xml.
Uri serviceUri = new Uri("http://fabrikam.com/service/getUser");
-
Note that if this service is hosted on a different domain than the Silverlight control, the service must opt in to cross-domain access if it is to be accessible to the control. For more information, see Making a Service Available Across Domain Boundaries.
-
Create a WebClient instance to make the HTTP GET request and download the response.
WebClient downloader = new WebClient();
-
Since WebClient only supports asynchronous requests, register a callback to call when the request is completed and make the request to the specified URI.
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(serviceUri);
-
Define the downloader_OpenReadCompleted event handler to get the HTTP response. Be sure to check the value of the >Error property to ensure that it is null and no errors were encountered while making the HTTP request.
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
}
}
-
Inside the if statement, get an Stream object that contains the response.
Stream responseStream = e.Result;
-
Depending on the type of data contained in the response, continue with one of the procedures described in Working with XML Data in Silverlight, Working with JSON Data in Silverlight, or Working with RSS or Atom Feeds.
Example
You should now have following code.
public void HowToMakeRequestsToHttpBasedServices()
{
Uri serviceUri = new Uri("http://fabrikam.com/service/getUser");
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(downloader_OpenReadCompleted);
downloader.OpenReadAsync(serviceUri);
}
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result;
// Continue working with responseStream here...
}
}