Поделиться через


How to: Specify Browser or Client HTTP Handling

Microsoft Silverlight will reach end of support after October 2021. Learn more.

With Silverlight, you can specify whether the browser or the client provides HTTP handling for your Silverlight-based applications. By default, HTTP handling is performed by the browser and you must opt-in to client HTTP handling. 

Client HTTP handling provides greater flexibility for sending request headers, enables you to call HTTP methods other than GET and POST, and access the response message if an error occurs. However, there are advantages to using browser HTTP processing as well. The following table lists the differences between browser and client HTTP handling in Silverlight.

Feature

Browser HTTP Handling

Client HTTP Handling

Basic authentication

Yes

Yes

Caching

Yes

Not supported

Concurrent connections

Handled by browser

6

Cross-domain support

Requires policy file

Requires policy file unless application is trusted (Silverlight 4)

Digest authentication

Handled by browser

Supported in Silverlight 4

Request methods

GET and POST

All if permitted per policy file

Response body

Only with 200 OK response

Fully supported

Response headers

Not supported

Fully supported

Status codes

Only 200 or 404

Fully supported

In addition to specifying whether to use browser or client HTTP handling, you can specify the scope for the handling. For example, you can specify HTTP handling for all messages, for a scheme (HTTP or HTTPS), for a particular domain, or for a single request object. Once you have specified the HTTP handling for a scheme or domain, you cannot change the HTTP handling for that domain or scheme. The following procedures describe how to specify browser or client HTTP handling, set the scope of the handling, and how to determine whether the browser or client is performing the handling for a request object.

NoteNote:

If you have not specified the HTTP handling for a particular scheme, domain, or request, HTTP handling for the scheme domain or request defaults to the browser.

To specify HTTP handling for all requests

  1. Register all HTTP and HTTPS URIs by using the RegisterPrefix method, passing a ClientHttp object to specify client HTTP handling or a BrowserHttp object to specify browser HTTP handling. The following code shows how to specify the client for handling all HTTP and HTTPS requests and responses.

    Dim httpResult As Boolean = WebRequest.RegisterPrefix("http://", _
        WebRequestCreator.ClientHttp)
    Dim httpsResult As Boolean = WebRequest.RegisterPrefix("https://", _
        WebRequestCreator.ClientHttp)
    
    bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
    bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
    
  2. Check the RegisterPrefix operation for success, and use the WebClient or HttpWebRequest as you normally would.

    If httpResult = True Then
        Dim wc As New WebClient()
        wc.OpenReadAsync(New Uri( _
            "http://api.search.live.net/qson.aspx?query=Silverlight"))
        AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted
    
    if (httpResult == true)
    {
        WebClient wc = new WebClient();
        wc.OpenReadAsync(new Uri("http://api.search.live.net/qson.aspx?query=Silverlight"));
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
    }
    

To specify HTTP handling for a domain

  1. Register the domain hosting services that you want to access by using the RegisterPrefix method, passing a ClientHttp object to specify client HTTP handling or a BrowserHttp object to specify browser HTTP handling. The following code shows how to specify the browser for handling all HTTP and requests and responses.

    ' Register the domain containing services you want to access with 
    ' Silverlight client. 
    Dim httpResult As Boolean = WebRequest.RegisterPrefix( _
        "http://api.search.live.net", WebRequestCreator.BrowserHttp)
    
    // Register the domain containing services you want to access with
    // Silverlight client.
    bool httpResult = WebRequest.RegisterPrefix("http://api.search.live.net", WebRequestCreator.BrowserHttp);
    
  2. Check the RegisterPrefix operation for success, and use the WebClient or HttpWebRequest as you normally would. In this example, HTTP requests and responses to the specified domain are processed by the browser.

    If httpResult = True Then
        Dim wc As New WebClient()
        wc.OpenReadAsync(New Uri( _
            "http://api.search.live.net/qson.aspx?query=Silverlight"))
        AddHandler wc.OpenReadCompleted, AddressOf wc_OpenReadCompleted
    
    if (httpResult == true)
    {
        WebClient wc = new WebClient();
        wc.OpenReadAsync(new Uri("http://api.search.live.net/qson.aspx?query=Silverlight"));
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
    }
    

To specify HTTP handling for a single request object

  1. Use the IWebRequestCreate.Create method to create the request object and register the domain hosting the services that you want to access and the HTTP handling for that request. You do this by passing the desired domain as the URI parameter and a ClientHttp object to specify client HTTP handling or a BrowserHttp object to specify browser HTTP handling.

    ' Create a HttpWebRequest with the specific URL 
    ' you will access with Silverlight client. 
    Dim request As HttpWebRequest = DirectCast( _
        WebRequestCreator.ClientHttp.Create(New Uri( _
        "http://api.search.live.net/qson.aspx?query=Silverlight")), HttpWebRequest)
    
    // Create a HttpWebRequest with the specific URL 
    // you will access with Silverlight client.
    HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(
            "http://api.search.live.net/qson.aspx?query=Silverlight"));
    
  2. Use the HttpWebRequest you created as you normally would. In this example, the HTTP handling for the HttpWebRequest is performed by the client.

         ' Use the request as you normally would. 
          request.BeginGetResponse(New AsyncCallback(AddressOf ReadCallback), request)
    
    // Use the request as you normally would.
    request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
    

To determine HTTP handling for a request object

  1. Use the CreatorInstance property of WebRequest to get an IWebRequestCreate object .

    Dim request As HttpWebRequest = DirectCast(asynchronousResult.AsyncState,  _
        HttpWebRequest)
    Dim creator As IWebRequestCreate = request.CreatorInstance
    
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
    IWebRequestCreate creator = request.CreatorInstance;
    
  2. Call the GetType method on the IWebRequestCreate object to obtain whether the object is a ClientHttp or BrowserHttp

    Dim requestType As Type = creator.[GetType]()
    
    Type requestType = creator.GetType();