Search This Blog

Thursday, September 13, 2012

How to read content of a html page using c#

// Way 1
using System.Net;
//...
using (WebClient client = new WebClient ()) // WebClient class inherits IDisposable
{
// save content into a file client.DownloadFile("http://myweb.com/page.html", @"C:\localfile.html");

// Or you can get the file content without saving it:
string htmlCode = client.DownloadString("http://myweb.com/page.html");
//...
}
// Way 2
/***********Read From same Application************/
using System.IO;
StreamReader sr = new StreamReader(Server.MapPath("Status.htm"));
string strS = sr.ReadToEnd();


//Way 3
/***************Read From Secure Wdb Site***********************/
//Read from secure website 'https'

Dim strRptHtml As String
Dim objResponse As WebResponse
Dim objRequest As WebRequest = HttpWebRequest.Create(strURL + "status.htm") ''''https://newqa.nccecas.org/cecas/status.htm objResponse = objRequest.GetResponse()
Using sr1 As New StreamReader(objResponse.GetResponseStream())
strRptHtml = sr1.ReadToEnd()
sr1.Close()
End Using
objResponse.Close()

No comments:

Post a Comment