Solved

UPS shipping rates

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

21 Experts available now in Live!

Get 1:1 Help Now