如何使用Weblclient或C#中的httpclient从Windows 10上启用网站下载文件
我正在尝试使用C#中的WebClient和HTTPClient从英国政府官方网站下载文件。 以下是我想使用的网站正在使用 tls1.3
https://www.gov.uk/government/publications/the-uk-sanctions-list
已经使用了以下代码(最近可以很好地工作)
private string Download_https_HMT(string url, string filePath, string fileName)
{
// The stream of data retrieved from the web server
Stream strResponse;
// The stream of data that we write to the harddrive
Stream strLocal;
// The request to the web server for file information
HttpWebRequest webRequest;
// The response from the web server containing information about the file
HttpWebResponse webResponse;
string path, filename;
path = filePath;// ConfigurationSettings.AppSettings["PATH"];
filename = fileName;// ConfigurationSettings.AppSettings["FILENAME"];
//ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.SystemDefault; //
//ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls13 | System.Net.SecurityProtocolType.Tls12 | System.Net.SecurityProtocolType.Tls11 | System.Net.SecurityProtocolType.Tls;/// (SecurityProtocolType)3072;
//ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072 | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)12288;
//ServicePointManager.SecurityProtocol=(SecurityProtocolType)12288;
WebClient webClient = new WebClient();
webClient.Headers.Add(HttpRequestHeader.Accept, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
webClient.Headers.Add(HttpRequestHeader.ContentType, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
webClient.Credentials = CredentialCache.DefaultCredentials; //new NetworkCredential("username", "password", "domain");
ServicePointManager.Expect100Continue = true;
//ServicePointManager.SecurityProtocol =SecurityProtocolType.Tls12|SecurityProtocolType.Tls13;// (SecurityProtocolType)12288;// System.Net.SecurityProtocolType.Tls12;/// (SecurityProtocolType)3072;
webClient.DownloadFile(url, filePath + fileName);
////using (WebClient wcDownload = new WebClient())
////{
//// //WebClient wc_ = new WebClient();
//// //////wcDownload.Headers.Add(HttpRequestHeader.UserAgent, "Other");
//// //////wcDownload.Headers.Add(HttpRequestHeader.Accept, "application/xls");
//// //////wcDownload.DownloadFile(url, filePath+fileName);
//// ////ServicePointManager.Expect100Continue = true;
//// ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
//// ////ServicePointManager.DefaultConnectionLimit = 9999;
//// //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
//// // SecurityProtocolType.Tls11 |
//// // SecurityProtocolType.Tls12;
//// // Create a request to the file we are downloading
//// webRequest = (HttpWebRequest)WebRequest.Create(url);
//// // Set default authentication for retrieving the file
//// webRequest.Credentials = CredentialCache.DefaultCredentials;
//// // Retrieve the response from the server
//// webResponse = (HttpWebResponse)webRequest.GetResponse();
//// // Ask the server for the file size and store it
//// Int64 fileSize = webResponse.ContentLength;
//// // Open the URL for download
//// strResponse = wcDownload.OpenRead(url);
//// // Create a new file stream where we will be saving the data (local drive)
//// DirectoryInfo dirInfo = new DirectoryInfo(path);
//// if (!dirInfo.Exists)
//// dirInfo.Create();
//// strLocal = new FileStream(path + filename, FileMode.Create, FileAccess.Write, FileShare.None);
//// // It will store the current number of bytes we retrieved from the server
//// int bytesSize = 0;
//// // A buffer for storing and writing the data retrieved from the server
//// byte[] downBuffer = new byte[2048];
//// // Loop through the buffer until the buffer is empty
//// while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
//// {
//// // Write the data from the buffer to the local hard drive
//// strLocal.Write(downBuffer, 0, bytesSize);
//// }
//// strResponse.Close();
//// strLocal.Close();
//// return path + filename;
////}
///
return path + filename;
}
我 我得到的错误类似:
关闭了基础连接。发送
的错误
或
算法不匹配
匹配发表评论的代码,但我已发送它以显示我尝试过的不同数量的内容。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终,我成功地使用了以下HTTPClient,但并不适用于所有站点。
尽管它适用于上述链接,但它不适用于另一个链接
澳大利亚制裁清单
Finally I was successful in doing so, using following HttpClient but it is not applicable to all sites.
Although it work for the above link, its not working for another link
Australian Sanctions List