Solved

UPS shipping rates

Posted on 2004-10-12
2
968 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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