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
Solved

UPS API

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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