Click here to Skip to main content
16,015,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In my application I want to show the description of a page which is available in some URL.

Now I want to get the Sourcecode of that URL.

After getting the SourceCode, I will save it into database.

And I will display whenever I want.

I have tried

C#
Method 1:

WebRequest req = WebRequest.Create("http://www.google.com");
WebResponse res = req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream());
string html = sr.ReadToEnd();

Method 2:

string address = "http://www.google.com/";

using (WebClient wc = new WebClient())
{
     string content = wc.DownloadString(address);
}


In both methods I am facing an error. Either it's timed out exception or unable to connect to the website remotely.

Please help
Posted

Just in case, try it with my HttpDownloader, which I published in full source code (just one file) here: how to download a file from internet[^].

It worked for me in most tricky situations. The code is short, yet you can learn some useful techniques.

—SA
 
Share this answer
 
v2
Better you can use the Webclient class to simplify your task:

using System.Net;
using (WebClient client = new WebClient())
{
string htmlCode = client.DownloadString(http://somesite.com/default.html);
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900