[英]Download zip file without using webclient c#
想要在不使用 Web 客戶端的情況下下載 zip 文件,因為收到錯誤“遠程服務器返回錯誤:(403) 禁止”。 在傳遞正確的標頭后請求。
需要 webclient 和 httpwebrequest 的替代方案。
提前致謝 !
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
HttpWebResponse WebResponse = (HttpWebResponse)request.GetResponse();
using (HttpClient client = new HttpClient())
{
try
{
HttpResponseMessage response = await client.GetAsync(URL);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
// Above three lines can be replaced with new helper method below
// string responseBody = await client.GetStringAsync(uri);
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
您可以使用 ASP.NET Web API 版本中引入的HttpClient
,它也包含在 .NET Core 中。
https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.7.2
看起來您的服務器需要身份驗證才能下載 ZIP 文件 - 您可以使用HttpClient
執行此操作,具體取決於您的服務器需要哪種類型的身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.