Download file from URL to a string

前端 未结 5 491
渐次进展
渐次进展 2020-12-05 17:03

How could I use C# to download the contents of a URL, and store the text in a string, without having to save the file to the hard drive?

相关标签:
5条回答
  • 2020-12-05 17:32
    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);
    

    got it from MVA hope it helps

    0 讨论(0)
  • 2020-12-05 17:34

    Use a WebClient

    var result = string.Empty;
    using (var webClient = new System.Net.WebClient())
    {
        result = webClient.DownloadString("http://some.url");
    }
    
    0 讨论(0)
  • 2020-12-05 17:44

    See WebClient.DownloadString. Note there is also a WebClient.DownloadStringAsync method, if you need to do this without blocking the calling thread.

    0 讨论(0)
  • 2020-12-05 17:45
    string contents;
    using (var wc = new System.Net.WebClient())
        contents = wc.DownloadString(url);
    
    0 讨论(0)
  • 2020-12-05 17:48

    use this Code Simply

    var r= string.Empty;
    using (var web = new System.Net.WebClient())
           r= web.DownloadString("http://TEST.COM");
    
    0 讨论(0)
提交回复
热议问题