How to get the type of data from a URL


After implementing the basic version of simple downloader, I have spent a few hours googling to know how to get the type of my URL say <em>.mp3,</em>.mp4 etc.for sites such as daily motion etc who's URL don't have it appended at the end.This is because my Downloader works for these types but a link without specific types makes it to download a Kb's file having nothing to play.

Here is the code to determine the content-type to decide the *.extension for downloading:

WebClient myWebClient = new WebClient(); string datastring = myWebClient.DownloadString("http://www.dailymotion.com/video/x1viyeu_pakistani-actress-meera-reema-saima-dance-on-faisal-ahmed-music-album-launch_news"); NameValueCollection headers = myWebClient.ResponseHeaders; foreach (string key in headers.AllKeys) { Console.WriteLine("Header:{0},Value:{1}", key, headers[key]); }

It returned me a list of outputs on Console among which a line was:


Now i want to hear that how will this help me to counter the issue already described.

Suggestions please

Here is the code for downloader

private void downloadbtn_Click(object sender, EventArgs e) { WebClient myWebClient = new WebClient(); //Declarations for string objects string downloadURL, path; //raw URL taken from user downloadURL = this.downloadURL.Text; path = this.savePath.Text; Uri tmp = new Uri(downloadURL); string EndPathFileName = tmp.Segments.Last(); path = path + @"\" + EndPathFileName; //downloads file using async method myWebClient.DownloadFileAsync(tmp, path); downloadbtn.Text = "Download Started"; downloadbtn.Enabled = false; myWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged); myWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted); }


Usually there is a Content-Type header that can give you a hint of what file type to expect.

Many times the server will also provide information regarding the filename - see <a href="https://stackoverflow.com/questions/2015985/using-header-to-rewrite-filename-in-url-for-dynamic-pdf" rel="nofollow">this SO</a> on how it is usually done on the (PHP) server side.


according to this <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1" rel="nofollow">http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1</a>

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.


you can get a content-type and split it like:

var request = HttpWebRequest.Create("http://www.google.com") as HttpWebRequest; if (request != null) { var response = request.GetResponse() as HttpWebResponse; string contentType = ""; if (response != null) contentType = response.ContentType; int start = contentType.IndexOf('/'); int end = contentType.IndexOf(';', start); string yourext = contentType.Substring(start+1, (end - start)-1);//like mp3,png,txt }


  • How to get the type of data from a URL
  • Can we differentiate between frame and non-frame loads with Ci in firefox addon
  • URL split in C#?
  • -[__NSCFString objectForKey:]: unrecognized selector sent to instance issue
  • How to sort array of objects(latitude, longitude) by nearest distance?
  • Parse “key cannot be nil” error on [PFObject saveInBackground] (Cocoa)
  • Getting Data from Fetch Post that returns response data
  • NUnit crashing with NHibernate
  • Use simple xml to drive the Quartz Sheduler
  • Get Web Response Status Code properly
  • Use Webclient to download several files
  • How do you POST data to a multi-page google form?
  • Powershell v2.0 Using multiple threads
  • How to sort NameValueCollection using a key in C#?
  • Mongoose model is an empty object inside post hook of another model
  • Spring Flow: Pass object back and forth from between Main WebFlow and Subflow
  • Convert a NameValueCollection to a dynamic object [closed]
  • display selected value of the drop down in jsp page
  • ASP.NET QueryString without equals sign
  • node expressjs - parsing a multipart/form-data POST data using body-parser middleware
  • Need to selectively remove newline characters from a file using unix (solaris)
  • Any disadvantages of using JQTouch/Phonegap over Android API?
  • Group by month+year with counts
  • Is it possible to control programs with Javascript?
  • Error while re-opening sound file in python
  • Insert audio album in Android MediaStore
  • Save image as is in photo album using swift
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Content-Length header not returned from Pylons response
  • ilmerge with a PFX file
  • Why value captured by reference in lambda is broken? [duplicate]
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Why joiner is not used after Sequence generator or Update statergy
  • Can't mass-assign protected attributes when import data from csv file
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS