rascal
asked on
How to obtain city & state from a zipcode using UPS / USPS webservice?
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
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
What environment and language are you using for your web service client?
ASKER
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.
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.
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.
ASKER
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, sShipperZi p,sShipToZ ip,sWeight ,sPackagin gType,sPac kageDesc,s Descriptio n,sService ,sAction,s Country
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'?><AccessRequ est xml:lang='en-US'>" & _
"<AccessLicenseNumber>8B6C 7C27F3B1F8 F0</Access LicenseNum ber>" & _
"<UserId>????????</UserId> " & _
"<Password>????????</Passw ord>" & _
"</AccessRequest>" & _
"<?xml version='1.0'?>" & _
"<RatingServiceSelectionRe quest xml:lang='en-US'><Request> <Transacti onReferenc e>" & _
"<CustomerContext>" & sContext & "</CustomerContext>" & _
"<XpciVersion>1.0001</Xpci Version></ Transactio nReference >" & _
"<RequestAction>Rate</Requ estAction> <RequestOp tion>" & sRequestOption & "</RequestOption></Request >" & _
"<PickupType><Code>" & sPickupType & "</Code></PickupType>" & _
"<Shipment><Shipper><Addre ss><Postal Code>" & 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></Packaging Type>" & _
"<Description>" & sDescription & "</Description><PackageWei ght><Weigh t>" & sWeight & "</Weight></PackageWeight> </Package> " & _
"<ShipmentServiceOptions/> </Shipment >" & _
"</RatingServiceSelectionR equest>"
'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("MSXML 2.ServerXM LHTTP")
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-ur lencoded"
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.sele ctNodes("/ /ResponseS tatusCode" )
If NodeList.Item(0).Text <> "1" Then
response.write "Problem with UPS Query<p>"
response.write("responsexm l=" & 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.sele ctNodes("R atedShipme nt")
For x = 0 To NodeList.length - 1
'Service/Code
'TotalCharges/MonetaryValu e
response.write("<option>")
Response.Write "Service/Code: " & NodeList.Item(x).selectSin gleNode("S ervice/Cod e").Text & " - TotalCharges$" & NodeList.Item(x).selectSin gleNode("T otalCharge s/Monetary Value").Te xt & " - GuaranteedDaysToDelivery: " & NodeList.Item(x).selectSin gleNode("G uaranteedD aysToDeliv ery").Text
response.write("</option>" )
Next
response.Write("</select>" )
set mydoc=nothing
set xmlhttp=nothing
%>
--------------------------
<%
dim sPickupType,sRequestOption
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'?><AccessRequ
"<AccessLicenseNumber>8B6C
"<UserId>????????</UserId>
"<Password>????????</Passw
"</AccessRequest>" & _
"<?xml version='1.0'?>" & _
"<RatingServiceSelectionRe
"<CustomerContext>" & sContext & "</CustomerContext>" & _
"<XpciVersion>1.0001</Xpci
"<RequestAction>Rate</Requ
"<PickupType><Code>" & sPickupType & "</Code></PickupType>" & _
"<Shipment><Shipper><Addre
"<ShipTo><Address><Country
"<Service><Code>" & sService & "</Code></Service>" & _
"<Package><PackagingType><
"<Description>" & sDescription & "</Description><PackageWei
"<ShipmentServiceOptions/>
"</RatingServiceSelectionR
'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("MSXML
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-ur
xmlhttp.send strXML
responsexml = xmlhttp.responseText
Set mydoc=Server.CreateObject(
if not isobject(mydoc) then
response.write("failed to create Microsoft.xmlDOM")
response.End()
end if
mydoc.loadxml(responsexml)
Set NodeList = mydoc.documentElement.sele
If NodeList.Item(0).Text <> "1" Then
response.write "Problem with UPS Query<p>"
response.write("responsexm
'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.sele
For x = 0 To NodeList.length - 1
'Service/Code
'TotalCharges/MonetaryValu
response.write("<option>")
Response.Write "Service/Code: " & NodeList.Item(x).selectSin
response.write("</option>"
Next
response.Write("</select>"
set mydoc=nothing
set xmlhttp=nothing
%>
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.Dom Document")
or for MSXML 4:
Set mydoc=Server.CreateObject( "Msxm2.Dom Document.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.getCityByZipC ode(intZip Code)
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
By the way, you should not be using:
Set mydoc=Server.CreateObject(
on a server. Use a more recent version of the parser, such as:
MSXML 3:
Set mydoc=Server.CreateObject(
or for MSXML 4:
Set mydoc=Server.CreateObject(
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.getCityByZipC
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Just what I was looking for. Thanks!
Mark
Mark