使用C#WebClient在小文件上下载速度很慢

时间:2014-10-30 04:07:36

标签: c# webclient

我正在尝试使用WebClient类下载一系列文件,但即使下载一个小文件也非常慢。我不熟悉在C#中使用HTTP,所以我不确定为什么会这样。

我写的代码是:

private void GetPhoto()
{
    string url = "http://www.glam-ou-rama.co.uk/users/600/gallery/";
    long fileNumber = 39658719305555612;
    string fileExtension = ".jpg";
    string targetFile = url + fileNumber.ToString() + fileExtension;
    txt_Gross.Text = targetFile;
    if (RemoteFileExists(targetFile))
    {
        //create path 
        txt_Exists.Text = "TRUE";
        string finalPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\saved\\" + fileNumber + fileExtension;
        txt_Exists.Text = finalPath;
        using (System.Net.WebClient client = new System.Net.WebClient())
        {
            client.Proxy = null;
            client.DownloadFile(targetFile, @finalPath);
        }
    }


}

RemoteFileExists方法:

private bool RemoteFileExists(string url)
    {
        try
        {
            //Creating the HttpWebRequest
            System.Net.HttpWebRequest request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
            //Setting the Request method HEAD, you can also use GET too.
            request.Method = "HEAD";
            //Getting the Web Response.
            System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
            //Returns TURE if the Status code == 200
            return (response.StatusCode == System.Net.HttpStatusCode.OK);
        }
        catch
        {
            //Any exception will returns false.
            return false;
        }
    }

0 个答案:

没有答案