Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UPS shipping rates

Posted on 2004-10-12
2
Medium Priority
?
971 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
1 Comment
 
LVL 17

Accepted Solution

by:
AerosSaga earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

782 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