locked
WebClient content MIME type RRS feed

  • Question

  • Hello,

    I'm trying to download resources at runtime and handle them according to their type.  Unfortunately, as far as I can tell, there is no way to check what MIME type the WebClient stream is.

    My external resource handler is designed to handle XAP packages, direct DLL files, images, videos, etc.  It can potentially be any kind of resource, returned dynamically by a web service (i.e. GetResource.aspx?resourceId=12345).

    Am I missing a way to do this?

    Friday, July 9, 2010 10:16 AM

Answers

  • Hi Mikernet123,

    Here is a working sample to get the content-type with HttpWebResponse. 

                Uri url = new Uri("http://localhost:5860/VideoTestTestPage.aspx", UriKind.Absolute);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.BeginGetResponse(new AsyncCallback(WebComplete), req);
           void WebComplete(IAsyncResult a)
            {
                HttpWebRequest req = (HttpWebRequest)a.AsyncState;
                HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
                //get the content-type here.
                Dispatcher.BeginInvoke(() => TextBox.Text = "Get the response.");
            }

    Best regards,

    Jonathan
     

    Thursday, July 15, 2010 11:27 PM

All replies

  • I was semi-hopeful when I found the ResponseHeaders property on WebClient, but it throws NotImplementedException...seriously?? A base framework property just there for fun?

    I'm a bit surprised...MIME types are sort of fundamental to context exchange over the web, no?

    Friday, July 9, 2010 10:26 AM
  •  Hi Mikernet123,

    I suspect that what you really want is to get the Content-type from WebClient.  If yes, you'd better refer to this document to see the limiations.  Please feel free to correct me if I have misunderstood you.

    Best regards,

    Jonathan

    Thursday, July 15, 2010 5:53 AM
  • Those limitations are on setting or changing that header value.

    I want to GET the response content type, not set the request header.

    Getting the content-type response (a.k.a the content MIME type) and acting accordingly is the basis of the entire web.  File extensions and the format of the request really mean nothing.

    As I said, WebClient.ResponseHeaders throws NotImplementedException when the property is accessed, so this has nothing to do with restricted headers.  Did the SL developers just accidentally miss something? Why have the property there at all if you aren't going to implement it?

    Thursday, July 15, 2010 1:23 PM
  • I haven't had time to make up an example, so I'm just going from the msdn pages:
    http://msdn.microsoft.com/en-us/library/system.net.webclient_members%28v=VS.95%29.aspx

    If you have access to the WebResponse object through the GetWebResponse function, then you may be able to check it's ContentType

    Thursday, July 15, 2010 7:46 PM
  • Hi Mikernet123,

    Here is a working sample to get the content-type with HttpWebResponse. 

                Uri url = new Uri("http://localhost:5860/VideoTestTestPage.aspx", UriKind.Absolute);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.BeginGetResponse(new AsyncCallback(WebComplete), req);
           void WebComplete(IAsyncResult a)
            {
                HttpWebRequest req = (HttpWebRequest)a.AsyncState;
                HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(a);
                //get the content-type here.
                Dispatcher.BeginInvoke(() => TextBox.Text = "Get the response.");
            }

    Best regards,

    Jonathan
     

    Thursday, July 15, 2010 11:27 PM
  • Perfect, thanks - every example I've ever seen used WebClient to download files so I didn't know these other classes existed in Silverlight.

    I think a content-type property on the WebClient.OpenReadCompleted event args would be rather handy though - not sure what the proper channel is to make the feature request.

    Friday, July 16, 2010 3:27 PM