Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content from file from this URL?

Tags:

c#

url

I have this URL: URL from Google

When open link in new tab, the browser force me download it. After download, I get a text file named "s". But I want use C# access to this URL and get it's text, don't save it as a file to computer. Is any way to do this?

like image 960
Thanh Nguyen Avatar asked Sep 03 '12 00:09

Thanh Nguyen


People also ask

How do I find the contents of a file?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.


2 Answers

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()

like image 137
Josh Avatar answered Sep 20 '22 10:09

Josh


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); 
like image 28
Kyle Avatar answered Sep 19 '22 10:09

Kyle