How to get content from file from this URL?

var webRequest = WebRequest.Create(@"http://yourUrl");

using (var response = webRequest.GetResponse())
using(var content = response.GetResponseStream())
using(var reader = new StreamReader(content)){
    var strContent = reader.ReadToEnd();
}

This will place the contents of the request into strContent.

Or as adrianbanks mentioned below simply use WebClient.DownloadString()


Since this question and my previous answer is fairly old now, a more modern answer would be to use HttpClient from System.Net.Http

using System.Net.Http;

namespace ConsoleApp2
{
    class Program
    {
        async static void Main(string[] args)
        {
            HttpClient client = new HttpClient();
            string result = await client.GetStringAsync("https://example.com/test.txt");
        }
    }
}

If not within an async function, then:

string result = client.GetStringAsync("https://example.com/test.txt").Result;

Try this:

var url = "https://www.google.com.vn/s?hl=vi&gs_nf=1&tok=i-GIkt7KnVMbpwUBAkCCdA&cp=5&gs_id=n&xhr=t&q=thanh&pf=p&safe=off&output=search&sclient=psy-ab&oq=&gs_l=&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=be3c25b6da637b79&biw=1366&bih=362&tch=1&ech=5&psi=8_pDUNWHFsbYrQeF5IDIDg.1346632409892.1";

var textFromFile = (new WebClient()).DownloadString(url);

Tags:

C#

Url