Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use ASP to get Shipping rates from USPS

ASP with API's

Use ASP to get Shipping rates from USPS

by  schase  Posted    (Edited  )
This is similiar to the one for USPS. So refer back if my notes are somewhat off. This code is for the testing server. Like UPS you need to sign up for the WebTools for USPS.
http://www.usps.com/webtools/welcome.htm

This one just specifies $1.00 to use for handling charge (strChosen)

USPS actually had me just put password in the password field - they say they neither check nor require it - but it didn't work without it.

This one just looks for Parcel Post, Express Mail & Priority Mail.

Good luck :)

Code:
<%
strChosen="1.00"
strXML= "<?xml version='1.0'?><RateV2Request USERID='userID' PASSWORD='password'>"
strXML=strXML & "<Package ID='0'><Service>ALL</Service>"
strXML=strXML & "<ZipOrigination>10022</ZipOrigination>"
strXML=strXML & "<ZipDestination>20008</ZipDestination>"
strXML=strXML & "<Pounds>10</Pounds>"
strXML=strXML & "<Ounces>5</Ounces>"
'strXML=strXML & "<Container>Flat Rate Box</Container>"
strXML=strXML & "<Size>LARGE</Size>"
strXML=strXML & "<Machinable>TRUE</Machinable>"
strXML=strXML & "</Package>"
strXML=strXML & "</RateV2Request>" 
strXML=server.URLEncode(strXML)
strXML="API=RateV2&XML=" & strXML
Set strXMLhttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 
strXMLhttp.Open "POST","http://testing.shippingapis.com/ShippingAPITest.dll?",false 
strXMLhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
strXMLhttp.send strXML
strResponseMessage = strXMLhttp.responseText
intHTTPStatusCode = strXMLhttp.status
strHTTPStatusText = strXMLhttp.statusText 
'response.write strResponseMessage & "<BR>"
'response.write intHTTPStatusCode & "<BR>"
'response.write strHTTPStatusText & "<BR>"
'get the response
responsexml = strResponseMessage
Set mydoc=Server.CreateObject("Microsoft.xmlDOM") 
mydoc.loadxml(responsexml) 
Set NodeList = mydoc.documentElement.selectNodes("Package/Postage") 
For strCount = 0 To NodeList.length - 1 
Select case Left(NodeList.Item(strCount).selectSingleNode("MailService").Text,6)
case "Parcel"
Session("svUSPS0")="U.S.P.S. Parcel Post"
Session("svUSPSCost0")=(cdbl(NodeList.Item(strCount).selectSingleNode("Rate").Text)+cdbl(strChosen))
Session("svUPSOrder0")=1
strCount2=strCount2+1
CASE "Priori"
Session("svUSPS1")="U.S.P.S. Priority Mail"
Session("svUSPSCost1")=(cdbl(NodeList.Item(strCount).selectSingleNode("Rate").Text)+cdbl(strChosen))
Session("svUPSOrder1")=1
strCount2=strCount2+1
CASE "Expres"
Session("svUSPS2")="U.S.P.S. Express Mail"
Session("svUSPSCost2")=(cdbl(NodeList.Item(strCount).selectSingleNode("Rate").Text)+cdbl(strChosen))
Session("svUPSOrder2")=1
strCount2=strCount2+1
END SELECT
NEXT
session("svCount")=clng(strCount2)
response.write strCount2 & "<BR>"
'Create a select table from the response xml 
response.Write("<select name=""txtShipping"" class=""searchfield"">")  & vbcrlf
IF (request("txtShipping"))="0" or (request("txtShipping"))="" or (request("txtshipping"))="&nbsp;" or isNULL((request("txtshipping"))) THEN 
response.write "<Option value=""0"" selected><-- Select Shipping --></option>"  & vbcrlf
ELSE
response.write "<Option value=""0""><-- Select Shipping --></option>"  & vbcrlf
END IF
'TotalCharges/MonetaryValue
For strCount=0 to (clng(session("svCount"))-1) 
IF (request("txtShipping"))= Session("svUSPS"&strCount) THEN
response.write("<option value=""" & Session("svUSPS"&strCount) & """ selected>") 
ELSE
response.write("<option value=""" & Session("svUSPS"&strCount) & """>") 
END IF
response.write Session("svUSPS"&strCount) & " - " & formatcurrency((Session("svUSPSCost"&strCount)),2)
response.write("</option>") & vbcrlf 
Next 
response.Write("</select>") & vbcrlf
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top