Tuesday, January 20, 2009

Return HTML from a Web page (VB.NET & PHP)

If you need to get the HTML from a Web page to rip it, process it, and/or display it in the way you want, the following function is key:

Public Function GetHTML(ByVal sURL As String, ByVal e As Integer) As String
   Dim oHttpWebRequest As System.Net.HttpWebRequest
   Dim oStream As System.IO.Stream
   Dim sChunk As String
   oHttpWebRequest = (System.Net.HttpWebRequest.Create(sURL))
   Dim oHttpWebResponse As System.Net.WebResponse = oHttpWebRequest.GetResponse()
   oStream = oHttpWebResponse.GetResponseStream
   sChunk = New System.IO.StreamReader(oStream).ReadToEnd()
   oStream.Close()
   oHttpWebResponse.Close()
   If e = 0 Then
      Return Server.HtmlEncode(sChunk)
   Else
      Return Server.HtmlDecode(sChunk)
   End If
End Function

And here's how to do it in PHP

$file = file_get_contents('http://www.yoururl.com');

*snicker*

No comments: