0

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 .mp3,.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:

Header:Content-Type,Value:text/html;charset=utf-8

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);




    }

3 Answers3

0

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 this SO on how it is usually done on the (PHP) server side.

Community
  • 1
  • 1
Michael Banzon
  • 4,879
  • 1
  • 26
  • 28
0

according to this http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1

Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body.
sm.abdullah
  • 1,777
  • 1
  • 17
  • 34
0

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
        }
Yehia Elhawary
  • 578
  • 1
  • 4
  • 20
  • Does WebClient supports a method like this as i am using that throughout my project – Javed Iqbal Oct 04 '14 at 08:23
  • Your code doesn't determine it for https://archive.org/download/27/items/AllahHuAllahHu/AllahHuAllahHuimranShaikh.wmv – Javed Iqbal Oct 04 '14 at 10:24
  • this url return this error! The item you have requested has a problem with one or more of the metadata files that describe it, which prevents us from displaying this page. – Yehia Elhawary Oct 04 '14 at 23:46
  • What about the original issue;though i came to know how to get the contents out of responseheaders but what is its linkage now to resolve the issue for which i posted !!! – Javed Iqbal Oct 08 '14 at 08:57
  • well, a line such as: Header:Content-Type,Value:text/html;charset=utf-8 will help me how to use for the purpose @Yehia – Javed Iqbal Oct 08 '14 at 09:00
  • My code already working good,see this image http://im70.gulfup.com/cEmIJ4.png What is error? – Yehia Elhawary Oct 09 '14 at 12:40
  • it doesn't work for links like https://archive.org/download/27/items/AllahHuAllahHu/AllahHuAllahHuimranShaikh.wmv – Javed Iqbal Oct 09 '14 at 15:59
  • Also simple links work but others like http://www.dailymotion.com/video/x27hsb9_dunya-news-nation-celebrates-3rd-day-of-eid_news fails – Javed Iqbal Oct 09 '14 at 16:01
  • But for simple link(s) (where it works) how should it help me to solve my problem for which i posted .Moreover info like, Header:Content-Type,Value:text/html;charset=utf-8 ...how value should be used – Javed Iqbal Oct 09 '14 at 16:04
  • I have found a rather more sophisticated code at:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx but how will it help me @Yehia – Javed Iqbal Oct 09 '14 at 16:16