c# .net 4.0下载https文件

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    using System;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;

    public class HttpsDownloaderNet40
    {
        public bool DownloadFileWithProgress(string url, string savePath, ProgressBar progressBar, Label statusLabel)
        {
            try
            {
                // 配置 HTTPS 安全协议(.NET 4.0 支持 TLS 1.0/SSL 3.0)
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
                ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x0300 | (SecurityProtocolType)0x0C00;
                // 忽略 SSL 证书验证(仅测试环境,生产环境需移除)
                ServicePointManager.ServerCertificateValidationCallback +=
                    (sender, cert, chain, errors) => true;

                using (WebClient client = new WebClient())
                {
                    // 绑定进度更新事件(UI 线程安全)
                    client.DownloadProgressChanged += (sender, e) =>
                    {
                        if (progressBar != null && progressBar.InvokeRequired)
                        {
                            progressBar.Invoke((MethodInvoker)(() => UpdateProgress(progressBar, e.ProgressPercentage)));
                        }
                        else
                        {
                            UpdateProgress(progressBar, e.ProgressPercentage);
                        }

                        if (statusLabel != null && statusLabel.InvokeRequired)
                        {
                            statusLabel.Invoke((MethodInvoker)(() => UpdateStatus(statusLabel, e)));
                        }
                        else
                        {
                            UpdateStatus(statusLabel, e);
                        }
                    };

                    // 绑定下载完成事件
                    client.DownloadFileCompleted += (sender, e) =>
                    {
                        if (e.Error != null)
                        {
                            ShowMessageBox("下载失败:" + e.Error.Message, MessageBoxIcon.Error);
                        }
                        else
                        {
                            ShowMessageBox("文件下载完成!", MessageBoxIcon.Information);
                        }
                    };

                    // 开始异步下载(避免 UI 阻塞)
                    client.DownloadFileAsync(new Uri(url), savePath);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        // 辅助方法:更新进度条
        private void UpdateProgress(ProgressBar progressBar, int percentage)
        {
            if (progressBar != null)
            {
                progressBar.Value = percentage;
                progressBar.Update(); // 强制刷新(可选)
            }
        }

        // 辅助方法:更新状态标签
        private void UpdateStatus(Label statusLabel, DownloadProgressChangedEventArgs e)
        {
            if (statusLabel != null)
            {
                statusLabel.Text = $"下载进度:{e.ProgressPercentage}%  " +
                                 $"{e.BytesReceived}/{e.TotalBytesToReceive} 字节";
                statusLabel.Update(); // 强制刷新(可选)
            }
        }

        // 辅助方法:跨线程显示消息框
        private void ShowMessageBox(string message, MessageBoxIcon icon)
        {
            if (MessageBox.Show(message, "提示", MessageBoxButtons.OK, icon) == DialogResult.OK)
            {
                // 可选:下载失败后清理不完整文件
                // if (icon == MessageBoxIcon.Error && File.Exists(savePath)) File.Delete(savePath);
            }
        }
    }
     
}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

namespace WindowsFormsApp8

{

using System;

using System.IO;

using System.Net;

using System.Windows.Forms;

public class HttpsDownloaderNet40

{

public bool DownloadFileWithProgress(string url, string savePath, ProgressBar progressBar, Label statusLabel)

{

try

{

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;

ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0x0300 | (SecurityProtocolType)0x0C00;

ServicePointManager.ServerCertificateValidationCallback +=

(sender, cert, chain, errors) => true;

using (WebClient client = new WebClient())

{

client.DownloadFile(new Uri(url), savePath);

}

}

catch (Exception ex)

{

return false;

}

return true;

}

}

}

相关推荐
液态不合群1 小时前
C# 中比较实用的关键字,基础高频面试题!
开发语言·c#
小吴同学·2 小时前
NET6 WebApi第5讲:中间件(源码理解,俄罗斯套娃怎么来的?);Web 服务器 (Nginx / IIS / Kestrel)、WSL、SSL/TSL
中间件·c#·.net·.netcore·.net core
Front_Yue2 小时前
Unity中MonoBehaviour的生命周期详解
3d·unity·c#
monstercl2 小时前
C# 元组
开发语言·c#
Maybe_95272 小时前
nginx配置https域名后,代理后端服务器流式接口变慢
nginx·https·流式接口
中游鱼3 小时前
.NET Reactor 混淆 C# 的序列化和反序列化存在的问题
c#·.net
qq_297908016 小时前
asp.net进销存软件WEB进销存ERP软件库存玻璃行业
sqlserver·c#·.net·开源软件
工业甲酰苯胺6 小时前
C#实现自己的Json解析器(LALR(1)+miniDFA)
开发语言·c#·json
画个逗号给明天"6 小时前
C#从入门到精通(2)
开发语言·c#
吾与谁归in6 小时前
【C#】WinForm自定义控件及窗体
c#·winform·winform窗体样式