C#下载http文件详解编程语言

@(编程)

using System; 
using System.IO; 
using System.Net; 
 
namespace Wisdombud.Util 
{ 
    public class HttpHelper 
    { 
        public static bool DownloadFile(string localFile, string webFile) 
        { 
            bool flag = false; 
            long SPosition = 0; 
            FileStream FStream; 
            if (File.Exists(localFile)) 
            { 
                File.Delete(localFile); 
                FStream = File.OpenWrite(localFile); 
                SPosition = FStream.Length; 
                FStream.Seek(SPosition, SeekOrigin.Current); 
            } 
            else 
            { 
                FStream = new FileStream(localFile, FileMode.Create); 
                SPosition = 0; 
            } 
            try 
            { 
                HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(webFile); 
                if (SPosition > 0) 
                    myRequest.AddRange((int)SPosition); 
                Stream myStream = myRequest.GetResponse().GetResponseStream(); 
                byte[] btContent = new byte[512]; 
                int intSize = 0; 
                intSize = myStream.Read(btContent, 0, 512); 
                while (intSize > 0) 
                { 
                    FStream.Write(btContent, 0, intSize); 
                    intSize = myStream.Read(btContent, 0, 512); 
                } 
                FStream.Close(); 
                myStream.Close(); 
                flag = true; 
            } 
            catch (Exception) 
            { 
                FStream.Close(); 
                flag = false; 
            } 
            return flag; 
        } 
    } 
} 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20955.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论