0

I'm writing a program that performs certain actions based on the content of an URL. What's the best way to determine content type?

//pseudo code

WebClient c = new WebClient();
var data = c.DownloadData("http://mysite.com/download/2938923");
//var dataType = get data type

switch(dataType)
{
    case "pdf":
       //Run PDF
       break;
    case "doc":
       //Run Word
       break;
}
James
  • 2,811
  • 3
  • 25
  • 29

1 Answers1

1

Use the MIME type (returned as ContentType header with the request). This way is standards compliant.

string contentType = (c.ResponseHeaders[HttpResponseHeader.ContentType] ?? "").ToLower();
switch(contentType)
{
    case "application/pdf":
        // Run PDF
        break;
    case "text/plain":
        // Text file
        break;

    // etc ...
}
James
  • 2,811
  • 3
  • 25
  • 29
McGarnagle
  • 101,349
  • 31
  • 229
  • 260