Solved

How to obtain city & state from a zipcode using UPS / USPS webservice?

Posted on 2004-09-19
7
435 Views
Last Modified: 2012-05-05
Has anyone ever used either the UPS / USPS webservice to obtain the city and state for a given zipcode?

If so, (or if you have sample code for some other webservice that does it as well) could you please provide a sample?

Many thanks!
Mark
0
Comment
Question by:rascal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 12100276
What environment and language are you using for your web service client?
0
 
LVL 1

Author Comment

by:rascal
ID: 12104053
Our website is running IIS 5.0 and our site is ASP. From the web page, we want the user to be able to type in a zipcode and then have our vbscript use "MSXML2.ServerXMLHTTP" to send the formatted xml to the webservice and receive the responseText.
0
 
LVL 19

Expert Comment

by:drichards
ID: 12104902
ServerXMLHTTP is not a web service client - it is only for moving XML documents around.  You will have to either:

a) Write a web service client or find someone already using the service of interest and capture the request.  You will then hand-code the web service request, allowing for the parameter input, of course.  If you are able to read WSDL, you can build the web service request from scratch, but it's easier if you can just copy a working request.

b) Get a web service package for ASP.  I do not know of one offhand, but I'm sure someone will.  ASP.NET comes with web service stuff.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rascal
ID: 12104952
I am already using this technique on our site to fetch shipping costs from UPS. (See below for code already in use on our site). All I need to know is what values to use for <RequestAction> etc., in order to have the service return the city and state when provided with a zipcode.

-------------------------------
<%
dim sPickupType,sRequestOption,sContext,sShipperZip,sShipToZip,sWeight,sPackagingType,sPackageDesc,sDescription,sService,sAction,sCountry
sService="02"                ' type of service
sPickupType="01"
sShipperZip="98117"
sShipToZip="30041"
sWeight="33"
sPackagingType="02"
sCountry="Canada"
sRequestOption="rate"  'shop or rate
'sPackageDesc="Package"
sContext="Rating and Service"
'sDescription="Rate Shopping"

'dim all of our vars
Dim mydoc, responsexml,strXML
'you can build the query dynamically by using request.form or request.querystring, however you want to use it
strXML = "<?xml version='1.0'?><AccessRequest xml:lang='en-US'>" & _
         "<AccessLicenseNumber>8B6C7C27F3B1F8F0</AccessLicenseNumber>" & _
             "<UserId>????????</UserId>" & _
             "<Password>????????</Password>" & _
             "</AccessRequest>" & _
             "<?xml version='1.0'?>" & _
             "<RatingServiceSelectionRequest xml:lang='en-US'><Request><TransactionReference>" & _
             "<CustomerContext>" & sContext & "</CustomerContext>" & _
             "<XpciVersion>1.0001</XpciVersion></TransactionReference>" & _
             "<RequestAction>Rate</RequestAction><RequestOption>" & sRequestOption & "</RequestOption></Request>" & _
             "<PickupType><Code>" & sPickupType & "</Code></PickupType>" & _
             "<Shipment><Shipper><Address><PostalCode>" & sShipperZip & "</PostalCode></Address></Shipper>" & _
             "<ShipTo><Address><Country>" & sCountry & "</Country><PostalCode>" & sShipToZip & "</PostalCode></Address></ShipTo>" & _
             "<Service><Code>" & sService & "</Code></Service>" & _
             "<Package><PackagingType><Code>" & sPackagingType & "</Code><Description>" & sPackageDesc & "</Description></PackagingType>" & _
             "<Description>" & sDescription & "</Description><PackageWeight><Weight>" & sWeight & "</Weight></PackageWeight></Package>" & _
             "<ShipmentServiceOptions/></Shipment>" & _
             "</RatingServiceSelectionRequest>"
'this will url encode your request
'strXML = Server.UrlEncode(strXML)
'here's where we send the stuff
Dim xmlhttp,NodeList,x
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
if not isobject(xmlhttp) then
      response.write("failed to create MSXML2.ServerXMLHTTP")
      response.End()
end if

xmlhttp.Open "POST","https://www.ups.com/ups.app/xml/Rate?",false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send strXML

responsexml = xmlhttp.responseText
Set mydoc=Server.CreateObject("Microsoft.xmlDOM")
if not isobject(mydoc) then
      response.write("failed to create Microsoft.xmlDOM")
      response.End()
end if


mydoc.loadxml(responsexml)

Set NodeList = mydoc.documentElement.selectNodes("//ResponseStatusCode")
If NodeList.Item(0).Text <> "1" Then
      response.write "Problem with UPS Query<p>"
      response.write("responsexml=" & responsexml & "<p>")
      'response.end
End If


'Create a select table from the response xml
response.Write("<select name='shipping'>")
'Create A Nodelist of All The RatedShipments
Set NodeList = mydoc.documentElement.selectNodes("RatedShipment")
For x = 0 To NodeList.length - 1
'Service/Code
'TotalCharges/MonetaryValue
response.write("<option>")
Response.Write "Service/Code: " & NodeList.Item(x).selectSingleNode("Service/Code").Text & " - TotalCharges$" & NodeList.Item(x).selectSingleNode("TotalCharges/MonetaryValue").Text & " - GuaranteedDaysToDelivery: " & NodeList.Item(x).selectSingleNode("GuaranteedDaysToDelivery").Text
response.write("</option>")

Next
response.Write("</select>")

set mydoc=nothing
set xmlhttp=nothing
%>
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12108701
Download the SOAP toolkit from MSDN.  It contains a SOAP client you can use that's pretty easy to work with.

By the way, you should not be using:

Set mydoc=Server.CreateObject("Microsoft.xmlDOM")

on a server.  Use a more recent version of the parser, such as:

MSXML 3:
Set mydoc=Server.CreateObject("Msxm2.DomDocument")
or for MSXML 4:
Set mydoc=Server.CreateObject("Msxm2.DomDocument.4.0")

And to answer:

"All I need to know is what values to use for <RequestAction> etc., in order to have the service return the city and state when provided with a zipcode."

The WSDL will tell you this; it's the service interface definition.  The SOAP client can read the WSDL, too, and then you just do something like:

mySoapClient.getCityByZipCode(intZipCode)

or whatever their service uses.  That way you don't have to get so intimately involved with the XML itself.  It's really very easy to use.

Regards,
Mike Sharp

0
 
LVL 26

Accepted Solution

by:
rdcpro earned 100 total points
ID: 12108767
This blows me away, but it doesn't look like the USPS implements SOAP web services...to get the city/state from a zip code, you use a request like:

http://SERVERNAME/ShippingAPITest.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="xxxxxxx"%20PASSWORD="xxxxxxx"><ZipCode ID= "0"><Zip5>90210</Zip5></ZipCode></CityStateLookupRequest>

This page explains it all:

http://www.usps.com/webtools/htm/Address-Information.htm

Regards,
Mike Sharp
0
 
LVL 1

Author Comment

by:rascal
ID: 12109213
Just what I was looking for. Thanks!

Mark
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Extract XML Data from using TSQL 5 78
XML SQL 8 32
Powershell Regex Replace Question 5 43
How do i delete the last node in an xml in T-SQL 7 29
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question