Read in the contents of a webpage to a string.

September 02, 2010 - 07:20

You may wish to display the contents of a feed from another website or grab data from another website and perform some processing on this information and then redisplay on your own site.

The code below grabs the html code from the website stored in the myurl string and the returned webpage is saved into the webpage string.

<%@ Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.Net" %>

Dim myurl As String = ""
Dim webpage As String

Dim req As WebRequest = WebRequest.Create(myurl)

Dim result As WebResponse = req.GetResponse()
Dim ReceiveStream As Stream = result.GetResponseStream()

Dim read() As Byte = New Byte(512) {}
Dim bytes As Integer = ReceiveStream.Read(read, 0, 512)

While (bytes > 0)
Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-7")
webpage = webpage & encode.GetString(read, 0, bytes)
bytes = ReceiveStream.Read(read, 0, 512)
End While

Once the webpage string contains the data, you can do any string search and replace or indexof to find the data you are looking for.

I normally use this to grab data for weather or postcode lookup features where the website isn't providing an XML feed for the results.

© 2011