Silverlight: Accessing HTTP Web Services

Overview

There are two main classes used in Silverlight to access HTTP web services, WebClient and HttpWebRequest.

Using the WebClient Class

The WebClient class is suitable for most GET and POST requests and is easier to use than HttpWebRequest as it requires less code and due to its event-based model that returns data on the UI thread.

Download As a String

The following example shows how to download a file as a string and write the contents to the debug output.

C# Example
Visual Basic Example

Download As a Stream

The following example shows how to download a file as a stream and write the contents to the debug output. Normally you would use either the XmlReader or the DataContractJsonSerializer class to work with the response.

C# Example
Visual Basic Example

Make a POST Request

The following example shows how to make a POST request, download the result as a string, and write the contents to the debug output.

C# Example
Visual Basic Example

Using the HttpWebRequest Class

The HttpWebRequest supports a larger subset of the HTTP protocol, but uses a delegate-based callback mechanism and requires that any calls to the UI thread be marshaled appropriately. Please refer to the Silverlight documentation for details and samples.

Further Reading

Related information on the web is listed below.