0

I was trying to do something similar to the PHP command get_file_contents to get the market up a webpage, but it has to be in ASP.net. I wasnt sure if i could just open the webpage via file, and then just read the stream into another file.

One thought iw as thinking was: How to read an entire file to a string using C#? but i wasnt sure if that was the route i would be needing to go or if someone else knew something that would do what i wanted.

Looking for the answer to the following pseudocode:

$test = "www.google.com";
$string = get_file_contents($test);
$filePointer = fopen("sample.txt", "w+");
$filePointer.write($string);
fclose($filePointer);
Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

2 Answers2

3
string html;
using (var client = new WebClient())
{
    html = client.DownloadString("http://www.google.com");
}
Console.WriteLine(html);
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
  • what is reader in reference to? It seems in what he is writing vs his code sample is that between the stream and request, there is some sort of StreadReader reader ... but i am unsure as to how to link Stream with StreamReader. – Fallenreaper Oct 23 '12 at 15:06
  • both examples you had given work under 1 circumstance. They have to be non-secure pages. YOu get an authorization error for secure pages because cookies and such are not set. – Fallenreaper Oct 29 '12 at 13:58
  • @Fallenreaper I am not sure if you are referencing SSL or authentication. The former works fine with the code above. If you are talking about authentication, that was not mentioned in your question, and would require more specifics, as there are many ways of locking down web pages. – D'Arcy Rittich Oct 29 '12 at 14:10
  • I currently have a new question up, but i am going to reformulate it in a way which is easily understandable. The issue that i am working around is Authentication – Fallenreaper Oct 29 '12 at 15:19
-1

figured this might be what i was looking at: http://www.devprise.com/2006/07/14/c-method-to-mimic-php-file_get_contents/ Not sure though.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129