Programatically interact with webpage

HttpWebRequest and HttpWebResponse

I recommend the (Http)WebRequest and (Http)WebResponse classes within System.Net. If you look at the source of the web page you will find the names of the two fields - postcode1 and postcode2. You will need to create the POST data for the page, encoded correctly, and then attach it to the request.

Visual Basic:
'Imports:
Imports System.IO
Imports System.Net
Imports System.Web



Public Function GetPostcodeDistance( _
    ByVal startPostcode As String, _
    ByVal endPostcode As String _
) As String


    Const siteUrl As String = "http://www.dfes.gov.uk/cgi-bin/inyourarea/dist.pl"

    Dim webReq As HttpWebRequest
    Dim webResp As WebResponse
    Dim reqStm As StreamWriter
    Dim respStm As StreamReader
    Dim postData As String

    'Create web request
    webReq = DirectCast(WebRequest.Create(siteUrl), HttpWebRequest)
    webReq.Method = "POST"
    reqStm = New StreamWriter(webReq.GetRequestStream())

    'Write post data to the stream
    postData = "postcode1=" & HttpUtility.UrlEncode(startPostcode)
    postData &= "&postcode2=" & HttpUtility.UrlEncode(endPostcode)
    reqStm.Write(postData)

    'Send request and get response
    webResp = webReq.GetResponse()

    'Read from stream
    respStm = New StreamReader(webResp.GetResponseStream())

    'Parse HTML here

End Function

Good luck :cool:
 
Back
Top