views:

203

answers:

2

I'm having trouble setting any HTTP header values when making a HttpRequest from Silverlight 3.0?

Are HTTP Headers supported in Silverlight 3.0?

The following code throws an exception when the request callback is called:

        var url = new Uri("http://lonmw32795/RBSM_Portal_RESTfulWebService/HostInterrogationService.svc/host/environment");
        req = (HttpWebRequest)WebRequest.Create(url);
        req.Headers[HttpRequestHeader.Pragma] = "no-cache";
        req.Method = "GET";

        req.BeginGetResponse(new AsyncCallback(WebComplete), req);    

The exception type is System.NotSupportedException - WTF!

Anyone got any ideas?

Cheers

AWC

A: 

The simple answer is no it doesn't - if you want to create a HttpRequest you have to use the WebRequestCreator class.

check out this blog post for more info.

AWC
Do you mean that you need to use the ClientHttp in order to avoid the restrictions that come with using the BrowserHttp? We want people to find their questions already asked on SO and for the answers to be straightforward. It would help if you just added a little more detail to the answer.
AnthonyWJones
+2  A: 

Note the documentation for the WebHeaderCollection, most of the useful headers are restricted, it doesn't list pragma but then it also says that restricted headers is not limited to the list provided.

These restrictions are in place since the underlying browser would normally handle http requests and it would therefore control such headers using its own algorithms.

It may be possible to do this with the ClientHttp like this:-

    var url = new Uri("http://lonmw32795/RBSM_Portal_RESTfulWebService/HostInterrogationService.svc/host/environment");
    req = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(url);

Note that you will need to manage any cookies needed manually which may be a little tricky if the cookies are set as part of a response to requests that use BrowserHttp.

AnthonyWJones