hits counter
  Create Free Blog | Random Blog »   Report Abuse | Login   

 

Extracting HTML source from a URL website


Tags: , , ,

Was just thinking of trying something short and sweet and thought of trying out a snippet for extracting code from the entered url.
Following is the code have not declared the namespaces on top but used them directly in the code to bring more clarity on which namespace the object comes from.

The code is self explanatory so wont add any explanations over here..

<code>&lt;/// &lt;summary&gt;
/// Extracts the source from the url entered.
/// &lt;/summary&gt;
/// &lt;param name=&quot;url&quot;&gt;url to fetch the source from.&lt;/param&gt;
/// &lt;returns&gt;string: source for the url entered.&lt;/returns&gt;
public static string GetHtmlPageSource(string url)
{

System.IO.Stream st = null;
System.IO.StreamReader sr = null;

try
{
// make a Web request
System.Net.WebRequest req = System.Net.WebRequest.Create(url);

// get the response and read from the result stream
System.Net.WebResponse resp = req.GetResponse();
st = resp.GetResponseStream();
sr = new System.IO.StreamReader(st);
// read all the text in it
return sr.ReadToEnd();
}
catch (Exception ex)
{
return string.Empty;
}
finally
{
// close the stream &amp; reader objects.
sr.Close();
st.Close();
}
}</code>

<strong>UPDATE:</strong>

If you need to authenticate the request use the following just before you make the request to read the source
<code>
// authenticate using the credentials passed for getting access to the page.
if (username != null &amp;&amp; password != null)
req.Credentials = new System.Net.NetworkCredential(username, password);
// get the response and read from the result stream
.
.
.
</code>

Share SocialTwist Tell-a-Friend 
Generated Keywords for the page : n , asp net , vb net class ,

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image