UPS Online Tools (Address Validation and Rates and Service Selection)

Discussion in 'ASP .Net Web Services' started by jasonrawlins@gmail.com, Jun 15, 2006.

  1. Guest

    I didn't find very many examples of how to use UPS's Online Tools web
    service, so I wrote this code. The first example is all you need to
    send a request and receive a reply. This assumes you know how to create
    a properly formatted request and how to interpret the response. (You
    can find this information in the developer guides provided by UPS)

    using System;
    using System.IO;
    using System.Xml;
    using System.Xml.XPath;
    using System.Net;
    using System.Data;
    using System.Text;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Configuration;

    /// <summary>
    /// Contains static methods used to query UPS Online Tools
    /// </summary>
    public static class OnlineTool
    {
    /// <summary>
    /// Retrieves a response from UPS Online Tools. This method will work
    for any request type
    /// that is properly formatted.
    /// </summary>
    /// <param name="url">URL to the desired UPS Online Tool</param>
    /// <param name="request">The properly formatted XML request</param>
    /// <returns></returns>
    public static XmlDocument GetResponse(string url, string request)
    {
    // Convert the request to a byte[]
    byte[] byteArrayRequest = ConvertRequestToByteArray(request);

    // Create the HttpWebRequest
    HttpWebRequest httpWebRequest = CreateHttpWebRequest(url,
    request.Length);

    // Send the request.
    SendRequest(httpWebRequest, byteArrayRequest);

    // Get the response.
    HttpWebResponse httpWebResponse =
    (HttpWebResponse)httpWebRequest.GetResponse();

    // Create an XmlDocument from the response
    XmlDocument response = new XmlDocument();
    response.Load(httpWebResponse.GetResponseStream());

    httpWebResponse.Close();

    return response;
    }

    /// <summary>
    /// Creates the request header for querying UPS Online tools
    /// </summary>
    /// <param name="serverIdentity">Information required to connect to UPS
    Online Tools server</param>
    /// <returns>The XML formatted request header</returns>
    public static string CreateRequestHeader(ServerIdentity serverIdentity)
    {
    // Create the request header
    XmlDocument header = new XmlDocument();

    // Add the UPS identity information to the header
    header.LoadXml("<?xml version=\"1.0\"?><AccessRequest
    xml:lang=\"en-US\"><AccessLicenseNumber></AccessLicenseNumber>
    <UserId></UserId><Password></Password></AccessRequest>");

    header.SelectSingleNode(AccessRequest.XPaths.AccessLicenseNumber).InnerText
    = serverIdentity.AccessLicenseNumber;
    header.SelectSingleNode(AccessRequest.XPaths.UserId).InnerText =
    serverIdentity.UserID;
    header.SelectSingleNode(AccessRequest.XPaths.Password).InnerText =
    serverIdentity.Password;

    return header.OuterXml;
    }

    /// <summary>
    /// Converts the request to a byte[]
    /// </summary>
    /// <param name="request">The request to convert</param>
    /// <returns>byte[] representation of the request</returns>
    private static byte[] ConvertRequestToByteArray(string request)
    {
    ASCIIEncoding asciiEncodedRequest = new ASCIIEncoding();
    return asciiEncodedRequest.GetBytes(request);
    }

    /// <summary>
    /// Creates a HttpWebRequest object
    /// </summary>
    /// <param name="url">The URL to the desired UPS Online Tool</param>
    /// <param name="contentLength">Length of the request</param>
    /// <returns>An HttpWebRequestObject</returns>
    private static HttpWebRequest CreateHttpWebRequest(string url, int
    contentLength)
    {
    HttpWebRequest httpWebRequest =
    (HttpWebRequest)WebRequest.Create(url);

    httpWebRequest.Method = "POST";
    httpWebRequest.KeepAlive = false;
    httpWebRequest.UserAgent = "";
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    httpWebRequest.ContentLength = contentLength;

    return httpWebRequest;
    }

    /// <summary>
    /// Sends the request to UPS Online Tools
    /// </summary>
    /// <param name="httpWebRequest">The HttpWebRequest</param>
    /// <param name="byteArrayRequest">The byte[] representation of the
    request</param>
    private static void SendRequest(HttpWebRequest httpWebRequest, byte[]
    byteArrayRequest)
    {
    Stream requestStream = httpWebRequest.GetRequestStream();
    requestStream.Write(byteArrayRequest, 0, byteArrayRequest.Length);
    requestStream.Close();
    }
    }


    Here is the ServerIdentity Class:

    /// <summary>
    /// Contains identity information required to access UPS Online tools
    /// </summary>
    public class ServerIdentity
    {
    public string URL = "";
    public string AccessLicenseNumber = "";
    public string UserID = "";
    public string Password = "";

    public ServerIdentity() { }
    public ServerIdentity(string URL, string accessLicenseNumber, string
    userID, string password)
    {
    this.URL = URL;
    this.AccessLicenseNumber = accessLicenseNumber;
    this.UserID = userID;
    this.Password = password;
    }
    }
    , Jun 15, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bert
    Replies:
    1
    Views:
    574
    jmm-list-gn
    Sep 17, 2004
  2. shank

    UPS online tools

    shank, Feb 15, 2005, in forum: ASP General
    Replies:
    2
    Views:
    104
    shank
    Feb 15, 2005
  3. Ken VdB

    UPS Rates Online Tools

    Ken VdB, Jul 22, 2005, in forum: ASP General
    Replies:
    1
    Views:
    120
    Adrienne
    Jul 24, 2005
  4. asg

    ups rates

    asg, Jan 8, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    116
    Eric Bohlman
    Jan 9, 2006
  5. martinig
    Replies:
    0
    Views:
    172
    martinig
    Jul 2, 2007
Loading...

Share This Page