ASP.NET Core 使用 WebClient 从 URL 下载

本文使用 ASP .NET Core 3.1,但它在.NET 5、 .NET 6和.NET 8上也同样适用。如果使用较旧的.NET Framework,请参阅本文,不过,变化不大。

如果想要从 URL 下载任何数据类型,请参阅本文:HttpClient

使用WebClient.DownloadFile()是一种非常有效的方法来下载文件并将其保存在某处以进行处理。请注意,cookie 正在发送到服务器。

using (System.Net.WebClient wc = new System.Net.WebClient())

{

wc.Headers.Add("Cookie: Authentication=user"); // NOTE: add a cookie header to the request

try

{

string filename = System.Guid.NewGuid().ToString("N") + ".html"; // NOTE: use globally unique identifier for file name to avoid naming conflicts

wc.DownloadFile("http://www.prowaretech.com/", filename); // NOTE: could add a file extension here

// NOTE: do something with file

}

catch (System.Exception ex)

{

// NOTE: check exception object for the error

}

}

WebClient.DownloadString()对服务器内存的使用效率较低,但对于下载JSON来说效果很好。

using (System.Net.WebClient wc = new System.Net.WebClient())

{

try

{

wc.Headers["Accept"] = "application/json"; // NOTE: accept JSON code (optional)

string strJson = wc.DownloadString("http://www.prowaretech.com/json/some-file.json");

// NOTE: do something with "strJson"

}

catch (System.Exception ex)

{

// NOTE: check exception object for the error

}

}

WebClient.UploadString()用于提交 POST 请求并下载结果(在本例中为 JSON)。

using (System.Net.WebClient wc = new System.Net.WebClient())

{

try

{

wc.Headers["Accept"] = "application/json"; // NOTE: accept JSON code (optional)

wc.Headers["User-Agent"] = "Custom User-Agent"; // NOTE: set the user-agent header (optional)

string postBody = "field1=data1&field2=data2&field3=data3"; // NOTE: special characters in the data must be escaped

string strResult = wc.UploadString("http://www.prowaretech.com/some/endpoint", postBody);

// NOTE: do something with "strResult"

}

catch (System.Exception ex)

{

// NOTE: check exception object for the error

}

}

WebClient.DownloadData()是另一种效率较低的服务器内存使用方式。它将文件/端点/url 下载到字节数组中。

using (System.Net.WebClient wc = new System.Net.WebClient())

{

try

{

byte[] data = wc.DownloadData("http://www.prowaretech.com/file.html");

if (wc.ResponseHeaders != null)

{

string? contentType = wc.ResponseHeaders["Content-Type"];

// NOTE: do something with data and contentType (Content-Type should BEGIN with "text/html" in this case)

}

else

{

// NOTE: do something with data

}

}

catch (System.Exception ex)

{

// NOTE: check exception object for the error

}

}

WebClient.OpenRead()如果将整个流读入内存,则服务器内存的使用效率会降低。StreamReader.Read()在服务器上处理大型文件时,应谨慎使用内存。

using (System.Net.WebClient wc = new System.Net.WebClient())

{

try

{

using (System.IO.Stream stream = wc.OpenRead("http://www.prowaretech.com/"))

{

using (System.IO.StreamReader reader = new System.IO.StreamReader(stream))

{

string str = reader.ReadToEnd(); // NOTE: could read one character at a time with reader.Read() which is more efficient with regard to memory usage

// NOTE: do something with str

}

}

}

catch (System.Exception ex)

{

// NOTE: check exception object for the error

}

}

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
时光追逐者1 天前
一款基于 .NET 8 + Vue 开源的、企业级中后台权限管理系统
前端·vue.js·microsoft·开源·c#·.net·.netcore
时光追逐者2 天前
C#/.NET/.NET Core技术前沿周刊 | 第 33 期(2025年4.1-4.6)
c#·.net·.netcore
江沉晚呤时4 天前
如何深入理解C#中的备忘录模式(Memento Pattern)设计模式
运维·服务器·数据库·c#·.netcore
[email protected]4 天前
ASP.NET Core Web API 参数传递方式
后端·asp.net·.netcore
[email protected]4 天前
ASP.NET Core Web API 中 HTTP状态码的分类及对应的返回方法
http·asp.net·.netcore
全栈小58 天前
【C#】.net core 6.0 依赖注入常见问题之一,在构造函数使用的类,都需要注入到容器里,否则会提示如下报错,让DeepSeek找找原因,看看效果
c#·.netcore·依赖注入·deepseek
公子小六13 天前
ASP.NET Core WebApi+React UI开发入门详解
react.js·ui·c#·asp.net·.netcore
工藤新一OL13 天前
.netCore的winform程序如何调用webapi
c#·.net·.netcore·visual studio
江沉晚呤时14 天前
深入解析 C# 开闭原则(OCP):设计可扩展的系统
数据库·c#·系统安全·.netcore
江沉晚呤时16 天前
深入解析外观模式(Facade Pattern)及其应用 C#
java·数据库·windows·后端·microsoft·c#·.netcore