Calling a FedEx Rate Service from Classic ASP with SOAP call


Hi Friends,

Last month I worked in Classic ASP. I had to prepare a Delivery Calendar.

I consumed a FedEx web service for Rate request.

It can also return delivery commitment details like Delivery Date, Time, date etc.

Basically what I need is to call a web service using SOAP call.

May people find difficulty in Classic ASP, as FexEx is not giving sample code for consuming their web service.

So I decided to post at least on web service call, so that you guys can have idea about how to consume FedEx Web service in Classic ASP.

FedExCall.asp

<% option explicit %>
<!--#include file="FedexAccountInfo.asp"-->
<%
    Dim subscriberzip
    Dim subscribercountry
    Dim ShipmentDate
    Dim xmlReq
    Dim objhttp
    Dim outstr
    Dim NodeList

    subscriberzip = Request.QueryString("zip")
    subscribercountry = "US"
    ShipmentDate = Request.QueryString("date")

    xmlReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" &_
    "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">" &_
    "<soapenv:Body>" &_
    "<RateRequest xmlns=""http://fedex.com/ws/rate/v9"">" &_
    "<WebAuthenticationDetail>" &_
    "<UserCredential>" &_
    "<Key>" & FedExkey & "</Key>" &_
    "<Password>" & FedExPassword & "</Password>" &_
    "</UserCredential>" &_
    "</WebAuthenticationDetail>" &_
    "<ClientDetail>" &_
    "<AccountNumber>" & FedExAccountNumber & "</AccountNumber>" &_
    "<MeterNumber>" & FedExMeterNumber & "</MeterNumber>" &_
    "</ClientDetail>" &_
    "<TransactionDetail>" &_
    "<CustomerTransactionId>TEST</CustomerTransactionId>" &_
    "</TransactionDetail>" &_
    "<Version>" &_
    "<ServiceId>crs</ServiceId>" &_
    "<Major>9</Major>" &_
    "<Intermediate>0</Intermediate>" &_
    "<Minor>0</Minor>" &_
    "</Version>" &_
    "<ReturnTransitAndCommit>1</ReturnTransitAndCommit>" &_
    "<CarrierCodes>FDXE</CarrierCodes>" &_
    "<VariableOptions>SATURDAY_DELIVERY</VariableOptions>" &_
    "<RequestedShipment>" &_
    "<ShipTimestamp>" & ShipmentDate & "T09:00:00-00:00</ShipTimestamp>" &_
    "<DropoffType>REGULAR_PICKUP</DropoffType>" &_
    "<PackagingType>YOUR_PACKAGING</PackagingType>" &_
    "<Shipper>" &_
    "<Address>" &_
    "<PostalCode>96790</PostalCode>" &_
    "<CountryCode>US</CountryCode>" &_
    "</Address>" &_
    "</Shipper>" &_
    "<Recipient>" &_
    "<Address>" &_
    "<PostalCode>" & subscriberzip & "</PostalCode>" &_
    "<CountryCode>US</CountryCode>" &_
    "</Address>" &_
    "</Recipient>" &_
    "<ShippingChargesPayment>" &_
    "<PaymentType>SENDER</PaymentType>" &_
    "<Payor>" &_
    "<AccountNumber>" & FedExAccountNumber & "</AccountNumber>" &_
    "<CountryCode>US</CountryCode>" &_
    "</Payor>" &_
    "</ShippingChargesPayment>" &_
    "<RateRequestTypes>LIST</RateRequestTypes>" &_
    "<PackageCount>1</PackageCount>" &_
    "<PackageDetail>INDIVIDUAL_PACKAGES</PackageDetail>" &_
    "<RequestedPackageLineItems>" &_
    "<SequenceNumber>1</SequenceNumber>" &_
    "<Weight>" &_
    "<Units>LB</Units>" &_
    "<Value>10.0</Value>" &_
    "</Weight>" &_
    "</RequestedPackageLineItems>" &_
    "</RequestedShipment>" &_
    "</RateRequest>" &_
    "</soapenv:Body>" &_
    "</soapenv:Envelope>"

    set objHttp = Server.createobject("Msxml2.ServerXMLHTTP")

    'For live
    objHttp.open "POST", https://gateway.fedex.com:443/web-services/rate, false

    'For test
    'objHttp.open "POST", https://gatewaybeta.fedex.com:443/web-services/rate, false

    OBJHTTP.setRequestHeader "Referer", "Your Company name"
    OBJHTTP.setRequestHeader "Host", "wsbeta.fedex.com"
    OBJHTTP.setRequestHeader "Accept", "image/gif, image/jpeg,image/pjpeg, text/plain, text/html, */*"
    OBJHTTP.setRequestHeader "Content-Type", "image/gif"
    OBJHTTP.setRequestHeader "Content-Length", cstr(len(xmlReq))

    objHttp.Send xmlReq

    outstr = objHttp.responseText

    dim objDoc, i, j, status
    Set objDoc = CreateObject("Microsoft.XMLDOM")
    objDoc.async = False
    objDoc.LoadXml(outstr)

'    Response.Write objDoc.getElementsByTagName("v9:HighestSeverity")(0).text

    Set NodeList = objDoc.getElementsByTagName("v9:RateReplyDetails")

    for i=0 to (NodeList.length-1)
'        for j=0 to (NodeList(0).childNodes.length-1)
'            if Trim(NodeList(i).childNodes(j).nodename) = "v9:ServiceType" then
'                Response.write "ServiceType: " & Trim(NodeList(i).childNodes(j).text) & "<br/>"
'            elseif Trim(NodeList(i).childNodes(j).nodename) = "v9:DeliveryDayOfWeek" then
'                Response.write "Day: " & Trim(NodeList(i).childNodes(j).text) & "<br/>"
'            elseif Trim(NodeList(i).childNodes(j).nodename) = "v9:DeliveryTimestamp" then
'                Response.write "Date: " & left(Trim(NodeList(i).childNodes(j).text), 10) & "<br/>"
'                Response.write "Time: " & right(Trim(NodeList(i).childNodes(j).text), 8) & "<br/>"
'            elseif Trim(NodeList(i).childNodes(j).nodename) = "v9:DestinationServiceArea" then
'                Response.write "Area: " & Trim(NodeList(i).childNodes(j).text) & "<br/><br/>"
'            end if
'        next
    next

    if Len(outstr) = 0 then
        Response.write "<br/> Error: Unable to communicate with Fedex Server. Please check your Internet connection.<br/>"
    else
        Response.Write outstr
    end if
%>

Now run this page in Internet Explorer and you will see the response in XML.

Advertisements

16 thoughts on “Calling a FedEx Rate Service from Classic ASP with SOAP call

  1. Jacob

    Vikas,
    thanks for your script it saved me loads of time.
    How come when I try displaying the amount by adding –
    “elseif Trim(NodeList(i).childNodes(j).nodename) = “v9:Amount” then
    Response.write “Amount: ” & Trim(NodeList(i).childNodes(j).text) & “”
    end if”

    it doesn’t display?

    Reply
  2. siteboxpro

    Exactly what i was looking for. I have one question \ statement, the FedExAccountInfo.asp should contain the following:

    Is there anything else that is needed? (obviously VALUE is replaced by the actual value from the FedEx account).

    Reply
  3. Vishal

    Hi Vikas,

    I am trying the same code to get the xml response but getting the error “msxml3.dll: A connection with the server could not be established”.

    Can you please help me out on this.

    Reply
  4. Aaron

    Hi Vikas,

    This is great and just in time before FedEx stops their legacy servers. Now that you have been so helpful, please let us know how to get the dollar amounts as well (same question that Jacob asked). I have been working on this for days, but it just does not show the actual dollar value.

    Reply
    1. Vishal

      Me too faced the same problem but got an solution, its not an complicated one. Refer the xml reply and go (read) tag by tag you will get the Amount for each service type.

      Reply
      1. Ray

        Hi Vishal. can you post an example of how to do this? Havent been able to get it to return the dollar amounts.

  5. Teresa

    Can you do this for rate/v10? Also, on v10, when I change the url from “https://wsbeta.fedex.com:443/web-services/rate” to the production at
    “https://ws.fedex.com:443/web-services/rate” I then get authentication failed.

    Reply
  6. Vishal

    Hi Vikas,
    do you have any example for FedEx Ship Label generation using web services?
    please provide me.

    Thanks in advance.
    Vishal.

    Reply
    1. Vikas Post author

      It’s been long time since I worked with FedEx. I don’t have any more access to FedEx account and classic asp server access. Sorry bro.

      Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s