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;