下面的代码仅使用.net 2.0即可运行多年。现在它停止了,并且仅适用于.net 4.6或更高版本。保持不变。调试器在
上运行client.DownloadFileAsync(新的Uri(sourceURL),目标);
行。但不再下载任何内容。
using System;
using System.ComponentModel;
using System.Net;
using System.Windows.Forms;
namespace NewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
StartDownload();
}
private void StartDownload()
{
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
string destination = "Tools.exe";
string sourceURL = "https://mysiteexample.com/download/Tools.exe";
client.DownloadFileAsync(new Uri(sourceURL), destination);
}
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Done");
}
}
}
答案 0 :(得分:1)
感谢Henk,我能够获得有关我的问题的更多信息。我的ISP开始需要TLS 1.2。所以我必须添加以下代码。现在工作正常。
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
因此,得到的代码是。
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
string destination = "Tools.exe";
string sourceURL = "https://mysiteexample.com/download/Tools.exe";
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
client.DownloadFileAsync(new Uri(sourceURL), destination);
}