Solved

UPS API

Posted on 2011-03-21
3
1,197 Views
Last Modified: 2012-08-14
Using UPS, I need to get the rates of a package. I'm trying to use the Rates API using XML. When I run my code, I don't get a response at all, which I would think that I'm not calling the correct server. This code does work for calling a different example that I found. I tried to adapt it to UPS.

  Private Sub webRequestPost_UPS()
    Dim tmp_str As String

    Dim request As WebRequest = WebRequest.Create("http://onlinetools.ups.com/Rate")
    request.Method = WebRequestMethods.Http.Post

    'create POST data and convert it to a byte array
    Dim postData As String = "<?xml version=""1.0""?><AccessRequest xml:lange='en-US'>" _
      & "<AccessLicenseNumber>1234</AccessLicenseNumber><UserID>userid</UserID>" _
      & "<Password>pwd</Password></AccessRequest>"
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

    request.ContentType = "application/x-www-form-urlencoded" 'set the contenttype property of the WebRequest
    request.ContentLength = byteArray.Length  'set the contentlength property of the WebRequest

    Dim dataStream As Stream = request.GetRequestStream() 'get the request stream
    dataStream.Write(byteArray, 0, byteArray.Length)      'write the data to the request stream
    dataStream.Close()                                    'close the stream object

    '-------
    Dim response As WebResponse = request.GetResponse     'get the response
    'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 'display the status
    tmp_str = CType(response, HttpWebResponse).StatusDescription
    dataStream = response.GetResponseStream               'get the stream containing content returned by the server

    Dim reader As New StreamReader(dataStream)            'open the stream using a StreamReader for easy access
    Dim responseFromServer As String = reader.ReadToEnd   'read the content
    'Console.WriteLine(responseFromServer)                 'display the content
    tmp_str = responseFromServer

    'clean up the streams
    reader.Close()
    dataStream.Close()
    response.Close()

    Me.Panel1.GroupingText = tmp_str
  End Sub
0
Comment
Question by:prothera
  • 3
3 Comments
 

Author Comment

by:prothera
Comment Utility
I was calling the incorrect serve. Should be:

    Dim request As WebRequest = WebRequest.Create("https://wwwcie.ups.com/ups.app/xml/Rate")

But now I am getting the error:
0FailureHard10002The XML document is well formed but the document is not valid
0
 

Accepted Solution

by:
prothera earned 0 total points
Comment Utility
Got it finally working. If any of the tags are the wrong caps or tags that are not expected are not there, you will receive a server error from UPS. Here is what I used.

  Private Sub webRequestPost_UPS()
    Dim tmp_str As String

    Dim request As WebRequest = WebRequest.Create("https://wwwcie.ups.com/ups.app/xml/Rate")
    request.Method = WebRequestMethods.Http.Post

    'create POST data and convert it to a byte array
    Dim postData As String = "<?xml version=""1.0""?><AccessRequest xml:lange='en-US'>" _
      & "<AccessLicenseNumber>12345</AccessLicenseNumber><UserId>username</UserId>" _
      & "<Password>pwd</Password></AccessRequest>"

    'read up the xml test file
    Dim postData2 As String = ""
    Dim path As String = "P:\path\test\"
    Dim xml_reader As New System.IO.StreamReader(System.IO.File.Open(path & "XMLFile.xml", System.IO.FileMode.Open))
    postData2 = postData & xml_reader.ReadToEnd

    Me.Panel1.GroupingText = Server.HtmlEncode(postData2) ' postData & postData2
    Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData2) '(postData & postData2)

    request.ContentType = "application/x-www-form-urlencoded" 'set the contenttype property of the WebRequest
    request.ContentLength = byteArray.Length  'set the contentlength property of the WebRequest

    Dim dataStream As Stream = request.GetRequestStream() 'get the request stream
    dataStream.Write(byteArray, 0, byteArray.Length)      'write the data to the request stream
    dataStream.Close()                                    'close the stream object

    '-------
    Dim response As WebResponse = request.GetResponse     'get the response
    'Console.WriteLine(CType(response, HttpWebResponse).StatusDescription) 'display the status
    tmp_str = CType(response, HttpWebResponse).StatusDescription
    dataStream = response.GetResponseStream               'get the stream containing content returned by the server

    Dim reader As New StreamReader(dataStream)            'open the stream using a StreamReader for easy access
    Dim responseFromServer As String = reader.ReadToEnd   'read the content
    'Console.WriteLine(responseFromServer)                 'display the content
    tmp_str = responseFromServer

    'clean up the streams
    reader.Close()
    dataStream.Close()
    response.Close()

    Me.Panel2.GroupingText = Server.HtmlEncode(tmp_str)
  End Sub

'*** XMLFile.xml ****
<?xml version="1.0" ?>
<RatingServiceSelectionRequest>
      <Request>
            <RequestAction>Shop</RequestAction>
      </Request>
      <Shipment>
            <Shipper>
                  <Address>
                        <PostalCode>89521</PostalCode>
                        <CountryCode>US</CountryCode>
                  </Address>
            </Shipper>
            <ShipTo>
                  <Address>
                        <PostalCode>94550</PostalCode>
                        <CountryCode>US</CountryCode>
                  </Address>
            </ShipTo>
            <ShipFrom>
                  <Address>
                        <PostalCode>89521</PostalCode>
                        <CountryCode>US</CountryCode>
                  </Address>
            </ShipFrom>
            <Service>
                  <Code>14</Code>
            </Service>
            <Package>
                  <PackagingType>
                        <Code>00</Code>
                  </PackagingType>
                  <PackageWeight>
                        <Weight>23</Weight>
                  </PackageWeight>
            </Package>
      </Shipment>
</RatingServiceSelectionRequest>

0
 

Author Closing Comment

by:prothera
Comment Utility
Figured it out with a few emails from UPS.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
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…

763 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

6 Experts available now in Live!

Get 1:1 Help Now