Solved

UPS API

Posted on 2011-03-21
3
1,201 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

7 Experts available now in Live!

Get 1:1 Help Now