Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - 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 .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);




    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...