mercredi 29 avril 2015

How can i download a file faster?

I have made a download program in C#. It is a queue downloader. You can see how it works here: Click Express Downloader

Is there any faster method to download? Here is the method i use, it must support resume support.

private void Download(object startPoint)
    {
        try
        {
            try
            {
                //int startPointInt = Convert.ToInt32(startPoint);
                Int64 startPointInt = Convert.ToInt64(startPoint);
                webRequest = (HttpWebRequest)WebRequest.Create(url);
                webRequest.AddRange(startPointInt);
                webRequest.Credentials = CredentialCache.DefaultCredentials;
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                Int64 fileSize = webResponse.ContentLength;
                strResponse = webResponse.GetResponseStream();
                if (startPointInt == 0)
                {
                    strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Create, FileAccess.Write, FileShare.None);
                }
                else
                {
                    strLocal = new FileStream(txtPath.Text + "\\" + filename, FileMode.Append, FileAccess.Write, FileShare.None);
                }
                int bytesSize = 0;
                byte[] downBuffer = new byte[4096];
                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
                {
                    strLocal.Write(downBuffer, 0, bytesSize);
                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });

                    if (goPause == true)
                    {
                        break;
                    }
                }
            }
            catch { }
        }
        finally
        {

            strResponse.Close();
            strLocal.Close();
        }
    }

Aucun commentaire:

Enregistrer un commentaire