Download file from URL to a string

C#

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

string contents;
using (var wc = new System.Net.WebClient())
    contents = wc.DownloadString(url);

Solution 2 - C#

Use a WebClient

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

Solution 3 - C#

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

Solution 4 - C#

use this Code Simply

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

Solution 5 - C#

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 6 - C#

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChigginsView Question on Stackoverflow
Solution 1 - C#mmxView Answer on Stackoverflow
Solution 2 - C#CaffGeekView Answer on Stackoverflow
Solution 3 - C#Danko DurbićView Answer on Stackoverflow
Solution 4 - C#alireza aminiView Answer on Stackoverflow
Solution 5 - C#GARUDAView Answer on Stackoverflow
Solution 6 - C#Tono NamView Answer on Stackoverflow