ASP.net Fetch html from other website to Your own aspx web application

Additional to RSS solution i given in
http://vinodkotiya.blogspot.com/2010/07/how-to-show-live-internet-content.html

There is a way to get html data from other site in your aspx web application.
Here is the basic code.


Imports System.Net
Imports System.IO
Partial Class Default3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim sUrl As String = "any url you wanna access.com"

        Try
            ' get a response object of given url
            Dim httpWebRequest As HttpWebRequest = DirectCast(WebRequest.Create(sUrl), HttpWebRequest)
            Dim httpWebResponse As HttpWebResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)

            Dim outputStream As New StreamReader(httpWebResponse.GetResponseStream(), System.Text.Encoding.ASCII)

            ' this writes the content to browser
            ' you may want to parse a particular section of html ?
            Me.Page.Response.Write(outputStream.ReadToEnd())

            httpWebResponse.Close()
            outputStream.Close()
        Catch ex As System.Exception
            Dim sError As String = "Error in viewer.aspx" & vbLf
            sError = sError + "error loading [" + sUrl + "]" & vbLf
            sError = sError + "Exception = " + ex.ToString() + vbLf

            Throw (New Exception(sError))
        End Try
    End Sub

End Class

- Vinod Kotiya
www.vinodkotiya.com

Comments

Popular Posts