Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Services > UPS Online Tools (Address Validation and Rates and Service Selection)

Thread Tools

UPS Online Tools (Address Validation and Rates and Service Selection)
Posts: n/a
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,

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

// Get the response.
HttpWebResponse httpWebResponse =

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


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

header.SelectSingleNode(AccessRequest.XPaths.Acces sLicenseNumber).InnerText
= serverIdentity.AccessLicenseNumber;
header.SelectSingleNode(AccessRequest.XPaths.UserI d).InnerText =
header.SelectSingleNode(AccessRequest.XPaths.Passw ord).InnerText =

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
HttpWebRequest httpWebRequest =

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
private static void SendRequest(HttpWebRequest httpWebRequest, byte[]
Stream requestStream = httpWebRequest.GetRequestStream();
requestStream.Write(byteArrayRequest, 0, byteArrayRequest.Length);

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;

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
UPS service won't start/no UPS tab in Power Options jdimester Computer Support 6 01-23-2010 03:27 PM
ups rates asg Perl Misc 1 01-09-2006 12:57 PM
UPS Rates Online Tools Ken VdB ASP General 1 07-24-2005 08:34 AM
UPS online tools shank ASP General 2 02-15-2005 09:55 PM
UPS online tools -xml request and reply Bert XML 1 09-17-2004 06:52 PM