[英]Download File with WebClient class occurs to exception
由于我是 c# 的新手,我不明白为什么这不起作用。
我正在尝试从 maven 存储库下载文件( https://repo1.maven.org/maven2/ )
这是我的代码:
public static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
try
{
var wcl = new WebClient();
wcl.DownloadFile("https://repo1.maven.org/maven2/com/typesafe/akka/akka-actor_2.11/2.3.3/akka-actor_2.11-2.3.3.jar", "C:/minecraft_test/mcassets/libraries/test/test.file");
}
catch (Exception ex)
{
Console.WriteLine("error");
}
}
我遇到了异常:从传输 stream 接收到意外的 EOF 或 0 字节。
我尝试更改/删除该行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
但我仍然遇到同样的错误。
我尝试使用 pwoershell:
$webclient = new-object System.Net.WebClient;
$webclient.DownloadFile("https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar", "C:/minecraft_test/mcassets/libraries/test/test.jar");
它的工作......
我能做些什么来解决这个问题?
最好的问候,谢谢
看来,您的第二个参数有问题
wcl.DownloadFile("https://repo1.maven.org/maven2/com/typesafe/akka/akka-actor_2.11/2.3.3/akka-actor_2.11-2.3.3.jar", "C:/minecraft_test/mcassets/libraries/test/test.file");
.
尝试删除路径中的“.file”,我相信它会解决问题。
此外,您不需要使用以下代码:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
好的,我找到了解决方案。
似乎 WebClient 已被弃用。 在 .net 文档中: https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netframework-4.8
我们可以读到:
重要的
我们不建议您使用 WebClient class 进行新开发。 而是使用 System.Net.Http.HttpClient class。
因此,我将代码更改为使用 HttpClient class。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.