Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

UPS API

Posted on 2011-03-21
3
Medium Priority
?
1,211 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
[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
  • 3
3 Comments
 

Author Comment

by:prothera
ID: 35185238
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
ID: 35194705
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
ID: 35194707
Figured it out with a few emails from UPS.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

670 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