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

J

jasonrawlins

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;
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top