52

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?

Chiggins
  • 8,197
  • 22
  • 56
  • 81

8 Answers8

87
string contents;
using (var wc = new System.Net.WebClient())
    contents = wc.DownloadString(url);
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • As noted by @CaffGeek you will want to dispose of the `WebClient` in a `using` block. – TrueWill Jul 08 '15 at 13:23
  • 3
    This is now Obsolete. When compiling VS gives this warning: `WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.` It works great though. – Tono Nam May 14 '22 at 22:57
16

Use a WebClient

var result = string.Empty;
using (var webClient = new System.Net.WebClient())
{
    result = webClient.DownloadString("http://some.url");
}
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
5

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

Danko Durbić
  • 7,077
  • 5
  • 34
  • 39
4

For more simpler and none-obsolete solution:

    public static string Download(string url)
    {
        using var client = new HttpClient();
        var content = client.GetStringAsync(url).Result;
        return content;
    }
Fidel
  • 7,027
  • 11
  • 57
  • 81
  • Don't use `Result` like that. It **not only synchronously waits (thus blocks UI) but also has a potential to cause a deadlock**. Don't call asynchronous methods synchronously. [This post](https://stackoverflow.com/a/76582825/2638227) shows the correct usage. – Mustafa Özçetin Jun 29 '23 at 16:28
3

use this Code Simply

var r= string.Empty;
using (var web = new System.Net.WebClient())
       r= web.DownloadString("http://TEST.COM");
alireza amini
  • 1,712
  • 1
  • 18
  • 33
3
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

チーズパン
  • 2,752
  • 8
  • 42
  • 63
GARUDA
  • 31
  • 2
2

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;
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 1
    In your sync example I think you want `GetAwaiter().GetResult()`, it will give you the true exception rather than an aggregate exception. – The Muffin Man May 14 '22 at 23:32
  • Tono's sync example will work in both .NET Framework 4.8 and .NET 6 if the using statements are replaced with the older using (HttpResponseMessage response = client.GetAsync(url).Result)) { .. } style. – Skyfish Feb 23 '23 at 11:19
0

Starting in .NET 6, the WebRequest, WebClient and ServicePoint classes are obsolete. Use the System.Net.Http.HttpClient class instead. (Actually using HttpClient class as HTTP API is the preferred way for .NET Framework 4.5 onward.)

Use the following method to get string content of a URL:

public async Task<string> DownloadString(string url)
{
    HttpClient httpClient = new HttpClient();
    string content = await httpClient.GetStringAsync(url);
    return content;
}

To call:

string content = await DownloadString("https://stackoverflow.com");

For the sake of simplicity, the DownloadString() method creates the HttpClient instance on every call. But the recommended way is to create it via IHttpClientFactory interface.

Mustafa Özçetin
  • 1,893
  • 1
  • 14
  • 16