Click here to Skip to main content
Click here to Skip to main content

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 >

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.

Comments and Discussions

 
Generalweb services Pinmemberyyyyyy8716-Oct-06 7:58 
Generalopening database Pinmemberyyyyyy8725-Sep-06 6:05 
Generalre:re: Looping Pinmemberyyyyyy8720-Sep-06 5:33 
Generalre:Looping Pinmemberyyyyyy8720-Sep-06 4:46 
Generallooping Pinmemberyyyyyy8719-Sep-06 12:31 
GeneralRe: looping Pinmemberrader1919-Sep-06 13:12 
GeneralRe: looping Pinmemberyyyyyy8719-Sep-06 18:41 
GeneralRe: looping Pinmemberrader1920-Sep-06 0:41 
QuestionAdding vb code to Getting Zip+4 from USPS.com Pinmemberyyyyyy8714-Sep-06 7:27 
Generalreq.ContentType appears 2x Pinmemberwdjyjvoxmmb27-Apr-06 11:26 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 2 Nov 2005
Article Copyright 2005 by Ryan Rader
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid