Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.
Program= `Test igetZipCode.wbt` ; Author: Len Schoen ; Company: Broward County Library VersionDate= "02/14/2012"; To adjust to changes made to the USPS's website. ; VersionDate= "08/06/2008"; To adjust to changes made to the USPS's website. ; VersionDate= "09/21/2005" ; Purpose: This tests the user defined function "igetZipcode" ; by submitting the main library's address to the USPS's website ; and if successful should receive official mailing address. ; ;------------------------------------------------------------------------------ ; GoSub Initialize :Begin Add1 = "100 S ANDREWS AVE" Add2 = "" City = "FORT LAUDERDALE" State = "FL" Zip = "33301" Add1 = AskLine(Program,`Enter street adr (include apt#):`,Add1) Ciry = AskLine(Program,`Enter city adr:`,City) State = AskLine(Program,`Enter State:`,State) Zip = AskLine(Program,`Enter Zip:`,Zip) Result = iGetZipcode(StrCat(Add1," ",Add2),"",City,State,Zip) Result = StrReplace(Result,@TAB,@CRLF) Pause(Program,StrSub(Result,1,400)) Goto Begin Exit ; ;============================================================================== :Initialize ;============================================================================== #DefineFunction igetZipcode (address1,address2,city,state,zipcode) ;------------------------------------------------------------------------------ ; Function to look up an address with the USPS website ;------------------------------------------------------------------------------ ; address1 (in) Street address with apartment number. ; address (in) usually not used; apt# (NOT building number) ; city (in) town, city or post office name. ; state (in) 2 char. state code. ; zipcode (in) Zipcode (optional) ; note: If Zipcode is entered, then city & state are optional. ; ; return address string format: ; street address@Tab -or- Error: <description> ; city@Tab ; state@Tab ; zipcode@Tab ; county ;------------------------------------------------------------------------------ ; Author: Len Schoen ; Company: Broward County Library (BCL) VersionDate= "02/14/2012"; To adjust to changes made to the USPS's website. ; VersionDate= "08/06/2008"; To adjust to changes made to the USPS's website. ; VersionDate= "09/21/2005" ;------------------------------------------------------------------------------ ; AddExtender("WWINT44i.DLL") ;Load the WinInet extender HTTP_STATUS_OK = 200 NotFound = -1 ; ; Do some preliminary data checking. If StrLen(address1)<5 Result = "Error: bad address; no street" Goto ReturnigetZipcode EndIf cityN = StrLen(city) stateN = StrLen(state) If stateN !=2 ; State code state = `` stateN = 0 EndIf zipcodeN = StrLen(zipcode) If zipcodeN>5 Then zipcode=StrSub(zipcode,1,5) If zipcodeN!=5 && zipcodeN!=0 zipcode = `` zipcodeN = 0 EndIf If zipcodeN==0 && (cityN==0 || stateN==0) Result = "Error: incomplete address" Goto ReturnigetZipcode EndIf ; ; Define key values ; host = `tools.usps.com` mainURL = `go/ZipLookupResultsAction!input.action?result` ; ; Allocate 100,000 bytes of buffer to hold the entire webpage size = 100000 buf = BinaryAlloc(size) ; ; Get address of buffer & initialize it bufadr = IntControl(42,buf,0,0,0) BinaryEodSet(buf,size) ; ; First define the FormData variable as a null string FormData = `` ; ; Then add the required form fields one at a time. FormData = iContentURL(FormData,`Mode`,`0`) FormData = iContentURL(FormData,`companyName`,``) FormData = iContentURL(FormData,`address1`,StrUpper(address1)) FormData = iContentURL(FormData,`address2`,StrUpper(address2)) FormData = iContentURL(FormData,`city`,StrUpper(city)) FormData = iContentURL(FormData,`state`,StrUpper(state)) FormData = iContentURL(FormData,`urbanCode`,``) FormData = iContentURL(FormData,`postalCode`,``) FormData = iContentURL(FormData,`zip`,StrUpper(zipcode)) FormData = iContentURL(FormData,`submit`,`Find ZIPCode`) ; ; Basic web page fetch script TopHandle = iBegin(0,``,``) connectHandle = iHostConnect(TopHandle,host,@HTTPS,``,``) DataHandle = iHttpInit(connectHandle,"POST",mainURL,``,0) Result = iHttpOpen(DataHandle,0,FormData,-1) ; ; If return status is not ok, return to calling program with error. If Result != HTTP_STATUS_OK headers = iHttpHeaders(DataHandle) AskItemlist("Result=%Result%",headers,@TAB,@UNSORTED,@SINGLE) GoSub CloseWebPage Result = "Error: USPS server rejected request; try again!" Goto ReturnigetZipcode EndIf ; ; Read the entire USPS webpage into a buffer NBytesRead = iReadDataBuf(DataHandle,bufadr,size) If NBytesRead==size ; BinaryWrite(buf,`TestZip.html`); for debugging copy results to HTML file. ; Pause(`TestZip.html`,`The results have been written.`) GoSub CloseWebPage Result = "Error: multiple addresses" Goto ReturnigetZipcode EndIf ; ; Determine if one address and only one address was found. If BinaryIndexEx(buf,0,"more than one address",@FWDSCAN,0)!=NotFound GoSub CloseWebPage Result = "Error: multiple addresses" Goto ReturnigetZipcode EndIf ; ; Determine If BinaryStrCnt(buf,0,NBytesRead-1,"mailingIndustryPopup2('C")>1 GoSub CloseWebPage Result = "Error: multiple addresses" Goto ReturnigetZipcode EndIf ; ; find the beginning and end of the address AdrBeg = BinaryIndexEx(buf,0,`Here's the full address, using standard abbreviations and formatting...`,@FWDSCAN,1) If AdrBeg==NotFound GoSub CloseWebPage Result = "Error: Problem Locating Address" Goto ReturnigetZipcode EndIf AdrBeg = BinaryIndexEx(buf,AdrBeg+99,`<span class="address1 range">`,@FWDSCAN,1)+29 AdrEnd = BinaryIndexEx(buf,AdrBeg,`<p class="show-details">`,@FWDSCAN,1)-30 If AdrEnd==NotFound GoSub CloseWebPage Result = "Error: address unknown" EndIf If AdrEnd==NotFound Then Goto ReturnigetZipcode ; ; Move the address from the buffer into a string AdrStr = BinaryPeekStr(buf,AdrBeg,AdrEnd-AdrBeg-2) AdrStr = StrReplace(AdrStr,@TAB, `$`) AdrStr = StrReplace(AdrStr,`$$`, `$`) ; ; Extract County from the webpage AdrBeg = BinaryIndexEx(buf,AdrEnd+31,`<dt>County</dt>`,@FWDSCAN,1)+34 AdrEnd = BinaryIndexEx(buf,AdrBeg,` </dd>`,@FWDSCAN,1) RCounty = BinaryPeekStr(buf,AdrBeg,AdrEnd-AdrBeg) ; ; Now that we have USPS address, get rid of webpage info GoSub CloseWebPage ; ; Remove anything not part of the address AdrStr = StrReplace(AdrStr,`;`, `$`) AdrStr = StrReplace(AdrStr,@TAB, `$`) AdrStr = StrReplace(AdrStr,@CRLF, ``);please do not change ; ;ClipPut(AdrStr); debug ;Pause(`AdrStr`,AdrStr);debug ; ; Parse the Street address. AdrEnd = StrIndex(AdrStr,`</span><br />`,0,@FWDSCAN)-1; end of Street adr RStreet = StrSub(AdrStr,1,AdrEnd) ; ; Parse the City address. AdrBeg = StrIndex(AdrStr,`<span class="city range">`,AdrEnd,@FWDSCAN)+25 AdrEnd = StrIndex(AdrStr,` </span>`,AdrBeg,@FWDSCAN) RCity = StrSub(AdrStr,AdrBeg,AdrEnd-AdrBeg) ; ; Parse the State adr. AdrBeg = StrIndex(AdrStr,`<span class="state range">`,AdrEnd,@FWDSCAN)+26 AdrEnd = AdrBeg + 2 ; always two letter code. RState = StrSub(AdrStr,AdrBeg,AdrEnd-AdrBeg) ; ; Parse the Zip adr (first 5). AdrBeg = StrIndex(AdrStr,`<span class="zip" style="">`,AdrEnd,@FWDSCAN)+27 AdrEnd = AdrBeg + 5 ; always 5 digits. RZip = StrSub(AdrStr,AdrBeg,AdrEnd-AdrBeg) ; ; Parse the Zip+4 adr. AdrBeg = StrIndex(AdrStr,`<span class="zip4">`,AdrEnd,@FWDSCAN)+19 AdrEnd = AdrBeg + 4 ; always 4 digits. RZip4 = StrSub(AdrStr,AdrBeg,AdrEnd-AdrBeg) ; ; Separate each field with a Tab delimiter Result = StrCat(RStreet,@TAB,RCity,@TAB,RState,@TAB,RZip,`-`,RZip4,@TAB,RCounty) ; :ReturnigetZipcode Return Result ; ;--------------------------------------------------------------- :CloseWebpage ; (part of UDF) ;--------------------------------------------------------------- buf = BinaryFree(buf) iClose(DataHandle) iClose(connectHandle) iClose(TopHandle) Return ; ;--------------------------------------------------------------- ; #EndFunction ;============================================================================== Return ;=========================================================================
Article ID: W17230
File Created: 2012:02:22:15:27:38
Last Updated: 2012:02:22:15:27:38