Solved

UPS shipping rates

Posted on 2004-10-12
2
967 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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