Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

 

'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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...