Tag: downloadfile

无法使用WebClient.DownloadFile方法从启用了TLS 1.1 / 1.2协议的计算机下载文件

我正在尝试使用Webclient.DownloadFile方法通过TLS 1.1 / 1.2协议实现一个简单的控制台应用程序来下载文件。 这是应用程序的代码: var downloadUrl = “https://serverURL.com/sample.mp3”; var filename = “sample.mp3”; var myWebClient = new WebClient(); myWebClient.DownloadFile(downloadUrl, filename); 每次运行它我都会收到以下错误消息: Unhandled Exception: System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. —> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possessa common algorithm at System.Net.SSPIWrapper.AcquireCredentialsHandle(SSPIInterface SecModule, String […]

正确处理两个WebException

我正在尝试正确处理两个不同的WebException 。 基本上他们在调用WebClient.DownloadFile(string address, string fileName) 到目前为止,AFAIK有两个我要处理的,都是WebException的: 无法解析远程名称(即没有网络连接访问服务器下载文件) (404)文件不正确(即服务器上不存在该文件) 可能会有更多,但这是我迄今为止最重要的。 那么我应该如何正确处理它,因为它们都是WebException ,但我想以不同的方式处理上面的每个案例。 这是我到目前为止: try { using (var client = new WebClient()) { client.DownloadFile(“…”); } } catch(InvalidOperationException ioEx) { if (ioEx is WebException) { if (ioEx.Message.Contains(“404”) { //handle 404 } if (ioEx.Message.Contains(“remote name could not”) { //handle file doesn’t exist } } } 正如您所看到的,我正在检查消息以查看它是什么类型的WebException。 我会假设有更好或更精确的方法来做到这一点? 谢谢