views:

966

answers:

3

Is there a way to get the Content-Type of the response or conversely set the Content-Type of an outgoing request in Silverlight using WebClient?

Edit

I need to make http requests and be able to show progress for them. I want to package the machinery for making the requests into a generic module and use it everywhere in my code. This I have already done. The difficulty seems to be when submitting different types of data to the server in POST I have no way to tell the server what the data is (json,xml,form encode,binary)

I believe I can deal with this by passing ?content-type=x with the request, and setting the server to prefer that over the Content-Type header.

I also have no way to know what kind of content the server responds with, I think I can solve this by passing the expected type of the data when I make a request.

If anyone has a better solution, please speak up :)

/Edit

Here's my predicament. The HttpWebRequest/Response objects implement an internal interface that allows for monitoring the progress of the request. If you want to do large requests it's very important that the user get to see a progress bar showing the status of the download/upload.

So using HttpWebRequest/Response is out. Leaving only WebClient, but I'm finding some odd things about it.

It cannot be subclassed. It's not actually sealed, but the constructor is marked [SecuritySafeCritical], which as far as I can tell, means I can't call it from a derived class. At least I failed, and found others on Google who had failed, but I would be very happy to be proved wrong on this point.

Internally it uses BrowserHttpWebResponse, which does not override the abstract Headers property, and WebClient.ResponseHeaders just forwards to m_Response.Headers, which just throws NotImplementedException.

Not sure Content-Type would even be in ResponseHeaders, but I would have liked to check.

It seems that we have the unhappy choice of having progress info or Content-Type info but not both in Silverlight.

According to the docs, there also seems no way to set Content-Type on the outgoing request either with WebClient. Content-Type is listed as a restricted header. I haven't actually tested this though.

Although it's interesting to note that on an error, you actually get passed the response object and have access to StatusCode, Content-Type, etc.

A: 

Have you tried using the WebClient and the DownloadProgressChanged event on the WebClient?

Michael S. Scherotter
Yes, as I said above, that seems to be the only way to get information about the download progress. But then the trouble is WebClient has a interface for dummies that doesn't expose anything more advanced (like Content-Type) so if you need progress and advanced features you really have zero options.
Eloff
A: 

You can do it like this if your server is asp.net or like this if it's php. These are solutions for upload progress, they might be able to be modified for download progress, but not easily.

The idea is that they rewrote the server code that does the upload to save the progress with an id, and then the client polls the server to get the current progress.

Mike Blandford
A: 

An easier solution might be to have the client/server code break the upload/download into chunks and send them one at a time. Then you can update your progress bar after each chunk. Of course, the smaller your chunk size the slower it will go.

Also: you could tell the server what content type it is via query string argument?

Mike Blandford
Actually I did fall back on a strategy like this, and it does sort of work.
Eloff