CODECUBE VENTURES

HTTP Screen Scraper Control

Using some code I found at aspng.com (I think), I made this very simple to use user control that hits any http address you want and spits out the HTML for you...


<script language="VB" runat="server">
private vurl as string
public property url as string
get
return vurl
end get
set
vurl = value
end set
end property

private Sub Page_Load(Src As Object, E As EventArgs)
if vurl <> "" then
Try
Dim objResponse As System.Net.WebResponse
Dim objRequest As System.Net.WebRequest
DIM theurlresult as string

objRequest = System.Net.HttpWebRequest.Create(url)
objResponse = objRequest.GetResponse()
Dim sr As new system.io.StreamReader(objResponse.GetResponseStream())
'theURLresult=server.HTMLencode(sr.ReadToEnd())
theURLresult=sr.ReadToEnd()
Page.Controls.Add(new LiteralControl(theURLresult))
Catch ex As Exception
Page.Controls.Add(new LiteralControl(ex.Message))
End Try
end if
End Sub
</script>

You can now use that to "include" any other website in your site. But ganking other people's content isn't the only reason for this, you can also make pseudo Web services this way. Take this Random article link generator I made for Basic Ultradev. It grabs a random record from SQL Server, and spits it out in XML Format. to consume it, just place this user control on your page, and set the url property to http://www.basic-ultradev.com/randarticle.asp.

here's an example of how to use this control.


<%@ register tagprefix="jm" tagname="scrape" src="../controls/scrape.ascx" %>
<script language="VB" runat="server">
sub page_load(o as object, e as eventargs)
if request.querystring("url") <> "" then
scraper.url = request.querystring("url")
end if
end sub
</script>
<jm:scrape id="scraper" url="http://www.basic-ultradev.com/randarticle.asp" runat="server" />

View the demo

Latest post: Digging Up the First Version of CodeCube

See more in the archives