C# System.Net.WebClient()使用在.netcore 3.1中工作的WebClient同步下载,但不使用.netcore 2.2(在代理之后)
我在我的办公机器上做了POC,下载了一个zip文件,解压缩并做了一些处理。我用一个控制台应用程序展示了这一点,但是,这需要转到azure功能应用程序。因此,我确保使用的是生产中运行的匹配框架(.netcore 2.2)。这完全符合预期 以下是下载该文件的代码片段:C# System.Net.WebClient()使用在.netcore 3.1中工作的WebClient同步下载,但不使用.netcore 2.2(在代理之后),c#,.net-core,azure-functions,webclient-download,C#,.net Core,Azure Functions,Webclient Download,我在我的办公机器上做了POC,下载了一个zip文件,解压缩并做了一些处理。我用一个控制台应用程序展示了这一点,但是,这需要转到azure功能应用程序。因此,我确保使用的是生产中运行的匹配框架(.netcore 2.2)。这完全符合预期 以下是下载该文件的代码片段: using System; using System.IO; using System.Net; using System.Threading.Tasks; namespace DownloadFile { class Pr
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace DownloadFile
{
class Program
{
private static string url = @"http://somesite.com/somefile.dat";
static async Task Main(string[] args)
{
try
{
Console.WriteLine("Downloading...");
await DownloadUsingWebClientAsync(url, Path.Combine(Path.GetTempPath(), "avvdat.ini"));
Console.WriteLine("Download complete");
}
catch(Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message.ToString());
}
Console.Write("Press ENTER key to end");
Console.ReadLine();
}
private static async Task DownloadUsingWebClientAsync(string url, string filePath)
{
using (var client = new WebClient())
{
await client.DownloadFileTaskAsync(url, Path.Combine(filePath));
}
}
}
}
当我在客户端的vm中运行相同的代码(控制台应用程序)时,这是失败的,错误如下(错误日志的SS)
很快,我意识到我们客户机的虚拟机在一个代理后面,并使用.pac(IE中的代理脚本文件)自动检测设置
为了使用代理设置,我对代码做了一些细微的更改
client.Proxy = new WebProxy("http://proxy.clientsite.com/internet_proxy.pac");
await client.DownloadFileTaskAsync(url, Path.Combine(filePath));
这给了我错误
我确信404与下载url无关,因为它没有改变/正确
现在,当我将框架更改为.netcore 3.1时,没有代理设置的代码正在运行
我不知道为什么会这样,但必须是3.0在内部负责代理设置。有没有一种方法可以使用2.2实现?因为我还没有被授权升级到3.1要求客户端从虚拟机中删除代理?这是不可能的,没有什么是不可能的。如果只有两个选项是升级到3.1(现在不支持3.0),或者删除代理,那么可能会让客户端决定。我已将3.0更改为3.1。所以你是说不可能在2.2中使用隐藏代理?
“http://proxy.clientsite.com/internet_proxy.pac“
是配置文件的URL,而不是代理URL。下载该文件并检查实际的URL地址。