Click here to Skip to main content
13,147,956 members (77,054 online)
Click here to Skip to main content
Add your own
alternative version

Stats

42.9K views
21 bookmarked
Posted 2 Nov 2005

Getting Zip+4 from USPS.com

, 2 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Getting Zip+4 from USPS.com

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!!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Ryan Rader
Web Developer
United States United States
Specialize in interfacing SAP and .net applications.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalweb services Pin
yyyyyy8716-Oct-06 7:58
memberyyyyyy8716-Oct-06 7:58 
Generalopening database Pin
yyyyyy8725-Sep-06 6:05
memberyyyyyy8725-Sep-06 6:05 
Generalre:re: Looping Pin
yyyyyy8720-Sep-06 5:33
memberyyyyyy8720-Sep-06 5:33 
OK. I put the Loop statement code in with no problem.(Please ignore my previuos message). But it still did not automate GetZip button. I still have to click on GetZip button to get zip codes each time. I have 2000 addresses that I need to get Zip code for. Currently, I have to click on GetZip button 2000 times. What I need is Once I click on GetZip button, It should return all the Zip codes in my dataset so that I don't have to keep clicking. Any solution would be appriciated.

thanks, Joseph

Try
For i As Integer = 0 To m14.Count - 1
If m14(i).Success Then
RemoveNonDigits = r1.Replace(m14(i).ToString, "")

If RemoveNonDigits <> "" Then
Dim m15 As Match = r13.Match(RemoveNonDigits)

If m15.Success Then
Return Convert.ToString(Regex.Replace(m15.ToString, "(\d{5})(\d{4})", "$1-$2"))
Else
Return "1"
End If
Else
Return "1"
End If
Else
Return "1"
End If
Next

programmer

Generalre:Looping Pin
yyyyyy8720-Sep-06 4:46
memberyyyyyy8720-Sep-06 4:46 
Generallooping Pin
yyyyyy8719-Sep-06 12:31
memberyyyyyy8719-Sep-06 12:31 
GeneralRe: looping Pin
rader1919-Sep-06 13:12
memberrader1919-Sep-06 13:12 
GeneralRe: looping Pin
yyyyyy8719-Sep-06 18:41
memberyyyyyy8719-Sep-06 18:41 
GeneralRe: looping Pin
rader1920-Sep-06 0:41
memberrader1920-Sep-06 0:41 
QuestionAdding vb code to Getting Zip+4 from USPS.com Pin
yyyyyy8714-Sep-06 7:27
memberyyyyyy8714-Sep-06 7:27 
Generalreq.ContentType appears 2x Pin
wdjyjvoxmmb27-Apr-06 11:26
memberwdjyjvoxmmb27-Apr-06 11:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170915.1 | Last Updated 2 Nov 2005
Article Copyright 2005 by Ryan Rader
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid