0


C#使用HttpWebRequest下载文件

public static bool HttpDownloadFile(string downloadUrl, string localPath, log4net.ILog log)
{
bool bFlagDownloadFile = false;
//log.Debug("HttpDownloadFile--准备以HTTP的方式下载文件,url:[" + downloadUrl + "]本地文件:【" + localPath + "】");

         HttpWebRequest request = null;
         HttpWebResponse resp = null;
         try
         {
             request = WebRequest.Create(downloadUrl) as HttpWebRequest;
             request.Timeout = 3000;
             //log.Info("HttpDownloadFile--新建HttpWebRequest!");

            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(delegate { return true; });
             resp = request.GetResponse() as HttpWebResponse;
             string responseCode = string.Empty;
             //获得http响应状态
             bFlagDownloadFile = getResponseResult(resp.StatusCode, ref responseCode);

            //获得HTTP响应的接收流
             Stream ns = resp.GetResponseStream();

            FileStream fs;
             fs = new FileStream(localPath, System.IO.FileMode.Create);
             int totalFileLen = 0;  //文件最多支持2^32 = 4G

            int fileBufferSize = 1024;
             byte[] nbytes = new byte[fileBufferSize];

            int nreadsize = ns.Read(nbytes, 0, fileBufferSize);
             while (nreadsize > 0)
             {
                 fs.Write(nbytes, 0, nreadsize);

                totalFileLen += nreadsize;
                 nreadsize = ns.Read(nbytes, 0, fileBufferSize);
             }

            fs.Close();
             ns.Close();
             bFlagDownloadFile = true;

            try
             {
                 if (resp != null)
                 {
                     resp.Close();
                     resp = null;
                 }
             }
             catch (Exception e)
             {

            }

            try
             {
                 if (request != null)
                 {
                     request.Abort();
                     request = null;
                 }
             }
             catch (Exception e)
             {

            }

            log.Debug("HttpDownloadFile--以HTTP的方式下载文件,本地文件:【" + localPath + "】成功!");
         }
         catch (Exception ex)
         {
             log.Error("HttpDownloadFile--以HTTP的方式下载文件,本地文件:【" + localPath + "】时发生错误!异常消息:" + ex.Message,ex);
             bFlagDownloadFile = false;

            try
             {
                 if (resp != null)
                 {
                     resp.Close();
                     resp = null;
                 }
             }catch(Exception e)
             {

            }

            try
             {
                 if (request != null)
                 {
                     request.Abort();
                     request = null;
                 }
             }
             catch (Exception e)
             {

            }
         }

        return bFlagDownloadFile;
     }

本文转载自: https://blog.csdn.net/daqinzl/article/details/142286664
版权归原作者 daqinzl 所有, 如有侵权,请联系我们删除。

“C#使用HttpWebRequest下载文件”的评论:

还没有评论