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;

}

}

}

相关推荐
张人玉3 小时前
c#中Random类、DateTime类、String类
开发语言·c#
future14124 小时前
游戏开发日记
数据结构·学习·c#
军训猫猫头6 小时前
3.检查函数 if (!CheckStart()) return 的妙用 C#例子
开发语言·c#
daikaimiao6 小时前
https——TCP+TLS
网络协议·tcp/ip·https
yqcoder8 小时前
12. 说一下 https 的加密过程
网络协议·http·https
yngsqq8 小时前
netdxf—— CAD c#二次开发之(netDxf 处理 DXF 文件)
java·前端·c#
每日出拳老爷子8 小时前
[WinForms] 如何为 .NET Framework 4.8 窗体程序添加自定义图标
visualstudio·c#·.net
Yan90188 小时前
.net解析雷达拼图3.0组合反射率产品,并按经纬度裁剪绘制组合反射率图
.net
步、步、为营8 小时前
.net服务器Kestrel配置Nginx作为反向代理
服务器·nginx·.net
专注VB编程开发20年8 小时前
各版本操作系统对.NET支持情况(250707更新)
开发语言·前端·ide·vscode·.net