【问题标题】:Download file from URL to a string从 URL 下载文件到字符串
【发布时间】:2011-03-15 00:04:10
【问题描述】:

如何使用 C# 下载 URL 的内容,并将文本存储在字符串中,而无需将文件保存到硬盘驱动器?

【问题讨论】:

    标签: c#


    【解决方案1】:
    using System.IO;
    using System.Net;
    
    WebClient client = new WebClient();
    
    string dnlad = client.DownloadString("http://www.stackoverflow.com/");
    
    File.WriteAllText(@"c:\Users\Admin\Desktop\Data1.txt", dnlad);
    

    从 MVA 获得 希望对你有帮助

    【讨论】:

      【解决方案2】:

      简单地使用此代码

      var r= string.Empty;
      using (var web = new System.Net.WebClient())
             r= web.DownloadString("http://TEST.COM");
      

      【讨论】:

        【解决方案3】:
        string contents;
        using (var wc = new System.Net.WebClient())
            contents = wc.DownloadString(url);
        

        【讨论】:

        • 正如@CaffGeek 所指出的,您需要在using 块中处理WebClient
        【解决方案4】:

        WebClient.DownloadString。请注意,还有一个WebClient.DownloadStringAsync 方法,如果您需要在不阻塞调用线程的情况下执行此操作。

        【讨论】:

          【解决方案5】:

          使用网络客户端

          var result = string.Empty;
          using (var webClient = new System.Net.WebClient())
          {
              result = webClient.DownloadString("http://some.url");
          }
          

          【讨论】:

          • 迟了一分钟!
          猜你喜欢
          • 2012-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-27
          • 1970-01-01
          相关资源
          最近更新 更多