'Download file from URL to a string

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?



Solution 1:[1]

Use a WebClient

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

Solution 2:[2]

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

Solution 3:[3]

use this Code Simply

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

Solution 4:[4]

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

Solution 5:[5]

None Obsolete solution:

async:

var client = new HttpClient();
using HttpResponseMessage response = client.GetAsync(url).Result;
using HttpContent content = response.Content;
var r = await content.ReadAsStringAsync();

sync:

var client = new HttpClient();
using HttpResponseMessage response = client.GetAsync(url).Result;
using HttpContent content = response.Content;
var r = content.ReadAsStringAsync().Result;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 alireza amini
Solution 4 チーズパン
Solution 5 Tono Nam