Get http file size, download location, and URL in a label

一个人想着一个人 提交于 2019-12-11 05:35:02

问题


I am using this to download a file and get % info and completed info. I want to know how to get the size of the file being downloaded and the URL remote address and the local address where the file is being saved to.

private void Form1_Load_1(object sender, EventArgs e)
        {
     label21.Text = "Download in progress...";
            WebClient webClient = new WebClient();
     webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
     webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
     webClient.DownloadFileAsync(new Uri("http://www.somesite.com/Update/Updates.zip.010"), @"Updates.zip.010");
 }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage; //Progress Bar Handler
            label1.Visible = true;
            label1.Text = progressBar1.Value.ToString() + " %"; //Adds percent to a label
        }

private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            label11.Visible = true;
            label11.Text = "Done";
        }

回答1:


I just rewrote what Jay wrote as comments to his own question, so that it'll be easier to read:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://cdn.sstatic.net/stackoverflow/img/favicon.ico");
req.Method = "HEAD";
// HttpWebRequest.GetResponse(): From MSDN: The actual instance returned
// is an HttpWebResponse, and can be typecast to that class to access 
// HTTP-specific properties. 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
long len = resp.ContentLength;

Just realized it's exactly what's in How to get the file size from http headers (maybe this question should be marked as duplicate?).



来源:https://stackoverflow.com/questions/3741814/get-http-file-size-download-location-and-url-in-a-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!