Introduction
I must start by stressing that this is for educational purposes only!! Ok, now that I got that out of the way.
The purpose of this project is to demostrate how to extract data from a website.
Lets get right to the good stuff.....the code!
The Code
The first step is to format the url with all the required information.
<P><FONT color=#0000ff size=2>Function</FONT><FONT size=2> ReplaceSpaceWithPlusSign(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> vStr </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>) </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> strTemp </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Try</P></FONT><FONT size=2><P> strTemp = Replace(vStr, vbTab, " ")</P><P> strTemp = Replace(strTemp, vbCr, " ")</P><P> strTemp = Replace(strTemp, vbLf, " ")</P><P> strTemp = Replace(strTemp, " ", "+")</P><P></FONT><FONT color=#008000 size=2> ' Remove leading and trailing spaces</P></FONT><FONT size=2><P> strTemp = Trim(strTemp)</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> strTemp</P><P></FONT><FONT color=#0000ff size=2> Catch</FONT><FONT size=2> ex </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Exception</P><P> MsgBox("Function: ReplaceSpaceWithPlusSign" + vbCrLf + "Message: " + ex.Message, MsgBoxStyle.Critical, "Error")</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Try</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</FONT><FONT color=#0000ff size=2></P></FONT><FONT size=2><P>strAdd = ReplaceSpaceWithPlusSign(txtAddress.Text)</P><P>strCity = ReplaceSpaceWithPlusSign(Trim(txtCity.Text))</P><P>strState = ReplaceSpaceWithPlusSign(cmbState.Text)</P><P></FONT><FONT color=#008000 size=2>'USPS web address</P></FONT><FONT size=2><P>urlStr = "http://zip4.usps.com/zip4/zcl_0_results.jsp?visited=1&pagenumber=0&firmname=&address2=" _</P><P> + strAdd + "&address1=&city=" + strCity + "&state=" + strState + "&urbanization=&zip5=&submit.x=6&submit.y=15"</P></FONT>
Now that you have the correcly formatted URL pass it to a function to read the websites source code.
<FONT size=2><P></FONT><FONT color=#0000ff size=2>Public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</FONT><FONT size=2> ReadWebSite(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> URL </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>) </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> req </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> HttpWebRequest</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> res </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> HttpWebResponse</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> strContents </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> StrStream </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Stream</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> Cok </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Cookie</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> oWebResponse </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> WebResponse</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> oReturnStream </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Stream</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> oReturnStreamReader </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> StreamReader</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> myReq </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> HttpWebRequest</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> myResponse </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> HttpWebResponse</P><P> </P><P> req = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> res = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> strContents = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> StrStream = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> Cok = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> oWebResponse = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> oReturnStream = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> oReturnStreamReader = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> myReq = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P> myResponse = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>Try</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>' Connects to web site and gets cookie</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'*********************************************************************************************** </P></FONT><FONT size=2><P> myReq = </FONT><FONT color=#0000ff size=2>DirectCast</FONT><FONT size=2>(WebRequest.Create(URL), HttpWebRequest)</P><P> myReq.CookieContainer = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> CookieContainer</P><P> myResponse = </FONT><FONT color=#0000ff size=2>DirectCast</FONT><FONT size=2>(myReq.GetResponse, HttpWebResponse)</P><P> myResponse.Cookies = myReq.CookieContainer.GetCookies(myReq.RequestUri)</P><P> </P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> myResponse.Cookies.Count > 0 </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P> Cok = myResponse.Cookies(0)</P><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P> Cok = </FONT><FONT color=#0000ff size=2>Nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>' Constucts html request</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'*********************************************************************************************** </P></FONT><FONT size=2><P> req = </FONT><FONT color=#0000ff size=2>DirectCast</FONT><FONT size=2>(WebRequest.Create(URL), HttpWebRequest)</P><P> req.Accept = "*/*"</P><P> req.ContentType = "application/x-www-form-urlencoded"</P><P> req.AllowAutoRedirect = </FONT><FONT color=#0000ff size=2>True</P></FONT><FONT size=2><P> req.UserAgent = "Mozilla/4.0 (compatible;" + " MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)"</P><P> req.ContentType = "application/x-www-form-urlencoded" </FONT><FONT color=#008000 size=2>'"text/html"</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Not</FONT><FONT size=2> Cok </FONT><FONT color=#0000ff size=2>Is</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Nothing</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P> req.CookieContainer = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> CookieContainer</P><P> req.CookieContainer.Add(Cok)</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>' Retrieves HTML Response</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'*********************************************************************************************** </P></FONT><FONT size=2><P> oWebResponse = req.GetResponse()</P><P> oReturnStream = oWebResponse.GetResponseStream()</P><P> oReturnStreamReader = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> StreamReader(oReturnStream)</P><P> strContents = oReturnStreamReader.ReadToEnd().Trim()</P><P> oReturnStreamReader.Close()</P><P> myResponse.Close()</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> strContents</P><P></FONT><FONT color=#0000ff size=2>Catch</FONT><FONT size=2> ex </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Exception</P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> UCase(ex.Message) = UCase("The operation has timed-out.") </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P> MsgBox("Please try again" + vbCrLf + "Postal service website timed-out.", MsgBoxStyle.Information, "Website timed-out")</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> ""</P><P></FONT><FONT color=#0000ff size=2> ElseIf</FONT><FONT size=2> UCase(ex.Message) = UCase("The remote server returned an error: (500) Internal Server Error.") </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P> MsgBox("Please try again" + vbCrLf + "Postal service website returned an Internal Error.", MsgBoxStyle.Information, "Internal Error")</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> ""</P><P></FONT><FONT color=#0000ff size=2> ElseIf</FONT><FONT size=2> UCase(ex.Message) = UCase("Thread was being aborted.") </FONT><FONT color=#0000ff size=2>Or</FONT><FONT size=2> UCase(ex.Message) = UCase("Thread was being aborted") </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2> 'Do nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P> MsgBox("Function: ReadWebSite" + vbCrLf + "Message: " + ex.Message, MsgBoxStyle.Critical, "Error")</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> ""</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Try</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2>'***********************************************************************************************</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</P></FONT>
Now that you have the source code pass it to another function that uses regular expressions to find the zip code.
<FONT size=2><P></FONT><FONT color=#0000ff size=2>Public</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</FONT><FONT size=2> GetZipCodeFromWeb(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> str </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</FONT><FONT size=2>) </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> RemoveNonDigits </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2> 'Removes all none digits</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> r1 </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Regex = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Regex("[^\d]")</P><P></FONT><FONT color=#008000 size=2> 'String format on usps.com's web site</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> r12 </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Regex = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Regex("(\d{5})-(\d{4})")</P><P></FONT><FONT color=#008000 size=2> 'Finds all digits</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> r13 </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Regex = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Regex("\d\d\d\d\d\d\d\d\d")</P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> str <> "" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> m14 </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> MatchCollection = r12.Matches(str)</P><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> lstr </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>String</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Try</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> m14(1).Success </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P> RemoveNonDigits = r1.Replace(m14(1).ToString, "")</P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> RemoveNonDigits <> "" </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Dim</FONT><FONT size=2> m15 </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Match = r13.Match(RemoveNonDigits)</P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> m15.Success </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> Convert.ToString(Regex.Replace(m15.ToString, "(\d{5})(\d{4})", "$1-$2"))</P><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Catch</FONT><FONT size=2> ex </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Exception</P><P></FONT><FONT color=#0000ff size=2> If</FONT><FONT size=2> ex.TargetSite </FONT><FONT color=#0000ff size=2>Is</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Nothing</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Then</P></FONT><FONT size=2><P></FONT><FONT color=#008000 size=2> 'Do nothing</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P> MsgBox("Function: GetZipCodeFromWeb" + vbCrLf + "Message: " + ex.Message, MsgBoxStyle.Critical, "Error")</P><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Try</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Else</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2> Return</FONT><FONT size=2> "1"</P><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>If</P></FONT><FONT size=2><P></FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Function</P></FONT>
Thats it!!