Solved

UPS API

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

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…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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