Solved

UPS shipping rates

Posted on 2004-10-12
2
964 Views
Last Modified: 2008-10-13
I am having a hard time finding examples/code/tutorials for ASP.NET for interfacing with the UPS rates and services service to get shipping info. It used MSXML2.ServerXMLHTTP.3.0 and MSXML2.DOMDocument.4.0 with a custom parsed XML string before (in classic ASP).

I am just having problems converting this over to ASP.NET.

Please Help!

Thanks
Mike MIller


            upsURL = "https://www.ups.com/ups.app/xml/Rate"
            
            'Get the rates including all the flat rate with optional expediting
            strUpsXmlExpedited = xmlHeader & xmlPackages & xmlPackagesFlatRateExpedited & xmlFooter
            Call Diag("strUpsXmlExpedited", strUpsXmlExpedited)
            
            set objSrvHTTP = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
            objSrvHTTP.open "POST", upsURL, false
            objSrvHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
            objSrvHTTP.send(strUpsXmlExpedited)
            
            Call Diag("objSrvHTTP.statusText", objSrvHTTP.statusText)
            Call Diag("objSrvHTTP.responseXML.xml", objSrvHTTP.responseXML.xml)
            
            set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")
            xmlDoc.async="false"
            xmlDoc.loadxml(objSrvHTTP.responseXML.xml)
      
            
            Set objNodes = xmlDoc.SelectNodes("/RatingServiceSelectionResponse/RatedShipment")
            objNodesLength = objNodes.Length
            
            Redim arrUpsRatesServices((objNodesLength - 1), 3)
            For iNode = 0 to (objNodesLength - 1)
                  Call Diag("objNodes.Item(iNode).SelectSingleNode(""Service/Code"").Text", objNodes.Item(iNode).SelectSingleNode("Service/Code").Text)
                  If objNodes.Item(iNode).SelectSingleNode("Service/Code").Text = "03" Then
                        UpsGroundIndex = iNode
                        Call Diag("UpsGroundIndex", UpsGroundIndex)
                  End If
                  arrUpsRatesServices(iNode, UPS_SERVICE_CODE) = objNodes.Item(iNode).SelectSingleNode("Service/Code").Text
                  arrUpsRatesServices(iNode, UPS_SERVICE_NAME) = UPS_ServiceName(objNodes.Item(iNode).SelectSingleNode("Service/Code").Text)
                  
                  curUpsServiceCost = 0
                  curUpsServiceCost = Cdbl(objNodes.Item(iNode).SelectSingleNode("TotalCharges/MonetaryValue").Text)
                        
                  Call Diag("curUpsServiceCost", curUpsServiceCost)
                  
                  arrUpsRatesServices(iNode, UPS_SERVICE_COST) = Round((curUpsServiceCost * CDbl(intShippingMultiple)) + CDbl(OrderTotalHandling) + CDbl(OrderTotalFlatFee), 2)
                  Call Diag("arrUpsRatesServices(iNode, UPS_SERVICE_COST)", arrUpsRatesServices(iNode, UPS_SERVICE_COST))
      
                  If arrUpsRatesServices(iNode, UPS_SERVICE_COST) < dblMinimumShipping Then
                        arrUpsRatesServices(iNode, UPS_SERVICE_COST) = dblMinimumShipping
                  End If
            Next
            
            If len(xmlPackages) = 0 and len(xmlPackagesFlatRateExpedited) > 0 Then
                  arrUpsRatesServices(UpsGroundIndex, UPS_SERVICE_COST) = CDbl(OrderTotalHandling) + CDbl(OrderTotalFlatFee)
            End If
            
            objNodesLength = 0
            
            Set objSrvHTTP = Nothing
            Set xmlDoc = Nothing
            set objNodes = Nothing
0
Comment
Question by:MikeMiller
2 Comments
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 12292702
Imports System.Net

Public Class UPS

   
    Private _AccessLicenseNumber As String
    Private _UserId As String
    Private _Password As String
    Private _ShipperPostalCode As String = "85260"
    Private _ShipperCountryCode As String = "US"

    Public Property AccessLicenseNumber() As String
        Get
            Return _AccessLicenseNumber
        End Get
        Set(ByVal Value As String)
            _AccessLicenseNumber = Value
        End Set
    End Property
    Public Property UserId() As String
        Get
            Return _UserId
        End Get
        Set(ByVal Value As String)
            _UserId = Value
        End Set
    End Property
    Public Property Password() As String
        Get
            Return _Password
        End Get
        Set(ByVal Value As String)
            _Password = Value
        End Set
    End Property
    Public Property ShipperPostalCode() As String
        Get
            Return _ShipperPostalCode
        End Get
        Set(ByVal Value As String)
            _ShipperPostalCode = Value
        End Set
    End Property
    Public Property ShipperCountryCode() As String
        Get
            Return _ShipperCountryCode
        End Get
        Set(ByVal Value As String)
            _ShipperCountryCode = Value
        End Set
    End Property

    Public Sub New(ByVal AccessLicenseNumber As String, ByVal UserId As String, ByVal Password As String)
        _AccessLicenseNumber = AccessLicenseNumber
        _UserId = UserId
        _Password = Password
    End Sub

    Public Function DomesticShippingRate(ByVal ShipToPostalCode As String, ByVal WeightInPounds As Single) As Single
        ' For Production, change the url to "https://www.ups.com/ups.app/xml/Rate"
        Dim UPSRequest As System.Net.HttpWebRequest
        Dim UPSResponse As System.Net.HttpWebResponse
        Dim xmlResponse As New System.Xml.XmlDocument
        Dim AccessRequest, RateRequest As String
        Dim RequestStream As System.IO.Stream
        Dim RequestBytes As Byte()

        UPSRequest = CType(HttpWebRequest.Create("https://wwwcie.ups.com/ups.app/xml/Rate"), HttpWebRequest)
        UPSRequest.AllowAutoRedirect = False
        UPSRequest.Method = "POST"
        UPSRequest.ContentType = "application/x-www-form-urlencoded"
        UPSRequest.Accept = "True"
        UPSRequest.Timeout = 50000

        AccessRequest = "<?xml version=""1.0""?>" & "<AccessRequest xml:lang=""en-US"">" & _
        "<AccessLicenseNumber>" & _AccessLicenseNumber & "</AccessLicenseNumber>" & _
        "<UserId>" & _UserId & "</UserId>" & "<Password>" & _Password & "</Password>" & _
        "</AccessRequest>"

        RateRequest = "<?xml version=""1.0""?>" & "<RatingServiceSelectionRequest xml:lang=""en-US"">" & _
        "<Request><TransactionReference><CustomerContext>" & "Rating and Service" & "</CustomerContext>" & _
        "<XpciVersion>1.0001</XpciVersion></TransactionReference>" & _
        "<RequestAction>Rate</RequestAction><RequestOption>shop</RequestOption></Request>" & _
        "<PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address>" & _
        "<PostalCode>" & _ShipperPostalCode & "</PostalCode>" & _
        "<CountryCode>US</CountryCode></Address></Shipper>" & _
        "<ShipTo><Address><PostalCode>" & ShipToPostalCode & "</PostalCode><CountryCode>US</CountryCode>" & _
        "</Address></ShipTo><Service><Code>11</Code></Service><Package><PackagingType>" & _
        "<Code>02</Code><Description>Package</Description></PackagingType><PackageWeight><UnitOfMeasurement>" & _
        "<Code>LBS</Code></UnitOfMeasurement><Weight>" & WeightInPounds & "</Weight>" & _
        "</PackageWeight></Package></Shipment></RatingServiceSelectionRequest>"

        RequestStream = UPSRequest.GetRequestStream()
        RequestBytes = System.Text.Encoding.UTF8.GetBytes(AccessRequest & RateRequest)

        RequestStream.Write(RequestBytes, 0, RequestBytes.Length)
        RequestStream.Close()

        Try
            UPSResponse = CType(UPSRequest.GetResponse(), System.Net.HttpWebResponse)

            xmlResponse.Load(UPSResponse.GetResponseStream)
            If xmlResponse.DocumentElement.FirstChild.InnerText = "Rating and Service1.00011Success" Then
                Return xmlResponse.SelectSingleNode("/RatingServiceSelectionResponse/RatedShipment/TransportationCharges/MonetaryValue").InnerText
            Else
                Throw New UPSExceptions("The UPS server returned the following error: " & _
               xmlResponse.SelectSingleNode("/RatingServiceSelctionResponse/Response/ResponseStatusDescription").InnerText)
            End If
        Catch ex As Net.ProtocolViolationException
            Throw New UPSExceptions("A timeout occurred while contacting the UPS server.")
        Catch ex As Net.WebException
            Throw New UPSExceptions("The UPS server did not return any data. Please try again later.")
        End Try
    End Function

End Class

100% complete and verified working.

Aeros
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use PHP to save or display image returned by JSON call. 20 86
How do I get the id from URL? 19 53
Problem to go to Web page 2 91
SQL best practice question 3 36
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now