C#远程下载文件保存到本地

 public static bool DownLoad(string url, string path, string contentType)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.ServicePoint.Expect100Continue = false;
                req.Method = "GET";
                req.KeepAlive = true;
                req.ContentType = contentType;// "image/png";
                using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream reader = rsp.GetResponseStream())
                    {


                        using (FileStream writer = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            byte[] buff = new byte[512];
                            int c = 0; //实际读取的字节数
                            while ((c = reader.Read(buff, 0, buff.Length)) > 0)
                            {
                                writer.Write(buff, 0, c);
                            }
                        }
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                SystemLog.WriteAppLog("HttpRequestHelper.DownLoad", e.Message);
                return false;
            }

        }

//第二种方式

 public static bool DownLoad(string url, string path)

        {
            try
            {
                WebClient mywebclient = new WebClient();
                string direcotry = path.Substring(0, path.LastIndexOf('/'));
                if (!System.IO.Directory.Exists(direcotry))
                    System.IO.Directory.CreateDirectory(direcotry);
                mywebclient.DownloadFile(url, path);
            }
            catch (Exception e)
            {
                SystemLog.WriteAppLog("HttpRequestHelper.DownLoad", e.Message);
                return false;
            }
            return true;
        }

猜你喜欢

转载自blog.csdn.net/qq_35534449/article/details/80619988