当前位置: 首页 > article >正文

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
    }
}

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


http://www.kler.cn/a/538534.html

相关文章:

  • Git的使用
  • 神经网络(Neural Network)
  • 【大模型】Ubuntu下安装ollama,DeepSseek-R1:32b的本地部署和运行
  • 单例模式几种实现
  • Faveo Helpdesk存在目录遍历漏洞(CVE-2024-37700)
  • DeepSeek-r1和O1、O3mini谁更强?
  • Linux进阶——搭建http静态网站
  • Chatbox+阿里云免费秘钥打造专属自己的deepseek桌面客户端
  • 多智能体协作架构模式:驱动传统公司向AI智能公司转型
  • 如何利用Java和Kotlin实现动态网页内容抓取
  • 深度学习之CycleGAN算法解析
  • 前端布局与交互实现技巧
  • Redis05 - 性能调优和缓存问题
  • webpack配置之---上下文
  • 华为交换机堆叠配置
  • E卷-服务器广播-需要广播的服务器数量-(200分)
  • 爬虫必备 -> Selenium【详解篇】(下)
  • 一口气入门前端——HTML5入门
  • 机器学习数学基础:14.矩阵的公式
  • CloudPaste:基于 Cloudflare Workers 的在线剪贴板和文件分享服务
  • Vim 多窗口编辑及文件对比
  • python_json转yolo文件
  • 伪分布式Spark3.4.4安装
  • webview_flutter的使用
  • Vite 代理下的 POST 请求跨域问题排查与解决方案
  • 搭建linux qt5.6环境