5,118,316 members and growing! (16,435 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

United States Postal Service (USPS) Web Tools Wrapper

By johnnycantcode

United States Postal Service (USPS) Web Tools Wrapper is a .NET library you can use for address validation, retrieving rates and YES even printing label.
C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 19 Oct 2006
Updated: 9 May 2008
Views: 33,932
Announcements



Search    
Advanced Search
Sitemap
10 votes for this Article.
Popularity: 4.63 Rating: 4.63 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 30.0%
4
7 votes, 70.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

UPDATE: I am working on a FedEx, UPS and DHL version of this tool. The DHL version should be the next one I release. I will post them here on codeproject.com when I am finished but you can check www.johnnycantcode.com until then for updates and to contact me directly.

I was in need of a library that would integrate the USPS Web Tools into my windows form application. (This library will work in ASP.NET web applications also.) The USPS Web tools are fairly easy to integrate but I wanted to create a reuasable component I could use in future projects. Please keep in mind this is my first Codeproject article so go easy! :)

USPS Web Tools

Before you can use this library you will need to get a USPS Web Tools ID. You can do this by filling out this form. Note: If you are using the USPS Test servers then the data in the examples below are the only data that will work. If you change the address or anything else you will get an error. Once you have tested your application you can request your profile to be updated by USPS to access the production environment, where you can use any appropriate data.

Validate an Address

///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = new Address();
a.Address2 = "6406 Ivy Lane";
a.City = "Greenbelt";
a.State = "MD";

///By calling ValidateAddress on the USPSManager object
///You get an Address object that has been validate by the
///USPS servers
Address validatedAddress = m.ValidateAddress(a);

Get Zipcode

///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = new Address();
a.Address2 = "6406 Ivy Lane";
a.City = "Greenbelt";
a.State = "MD";
Address addressWithZip = m.GetZipcode(a);
string zip = addressWithZip.Zip;

Get City State from Zip

///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Address a = m.GetCityState("90210");
string city = a.City;
string state = a.State;

Track a package

///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
TrackingInfo t = m.GetTrackingInfo("EJ958083578US");

Get Shipping Labels

///Create a new instance of the USPS Manager class
///The constructor takes 2 arguments, the first is
///your USPS Web Tools User ID and the second is 
///true if you want to use the USPS Test Servers.
USPSManager m = new USPSManager("YOUR_USER_ID", true);
Package p = new Package();
p.FromAddress.Contact = "John Smith";
p.FromAddress.Address2 = "475 L'Enfant Plaza, SW";
p.FromAddress.City = "Washington";
p.FromAddress.State = "DC";
p.FromAddress.Zip = "20260";
p.ToAddress.Contact = "Tom Customer";
p.ToAddress.Address1 = "STE 201";
p.ToAddress.Address2 = "6060 PRIMACY PKWY";
p.ToAddress.City = "Memphis";
p.ToAddress.State = "TN";
p.WeightInOunces = 2;
p.ServiceType = ServiceType.Priority;
p.SeparateReceiptPage = false;
p.LabelImageType = LabelImageType.TIF;
p.PackageSize = PackageSize.Regular;
p.PackageType = PackageType.Flat_Rate_Box;
p = m.GetDeliveryConfirmationLabel(p);
Note: If you want to test the other label methods you will need to download the Guides from the USPS Web Tools site and use the sample data they provide.

It appears that USPS may have changed some of the behaviour of their web services. I will look into this and make changes to the code accordingly.



I have updated the source code to include the XML Parser version of the FromXML medthod on the Address object. The code was provided by viperguynaz. Thank you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

johnnycantcode


I work as an independent software architect and senior developer. I have worked on many large enterprise projects as well as small single user applications.
Occupation: Software Developer (Senior)
Company: Gologic Tech LLC.
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralTo get a Zip Code response you need to make this changememberdefwebserver10:52 29 Nov '07  
Generaldetails of USPS xmlmemberkcirnitram8:26 16 Oct '07  
GeneralAPI Authorization failuremembershruti basnet22:44 18 Aug '07  
GeneralRe: API Authorization failurememberzhedex8:14 17 Jan '08  
GeneralRe: API Authorization failurememberrtsang11:57 10 Feb '08  
AnswerRe: API Authorization failurememberzhedex13:30 12 Feb '08  
GeneralIs this possiblememberbinaryDigit@@15:12 27 Jul '07  
GeneralCode ErrorsmemberBob100387:42 20 Jul '07  
GeneralThank you..memberstephenpatten9:05 16 Jun '07  
GeneralZip Code in Address ObjectmemberMindCore5:26 13 Jun '07  
GeneralGetCityState & GetZipcodememberviperguynaz11:43 18 Apr '07  
QuestionUSPS Approval For Windows ApplicationmemberMidnightRider31113:12 16 Apr '07  
AnswerRe: USPS Approval For Windows Applicationmembersesmith2k213:31 16 Apr '07  
QuestionRe: USPS Approval For Windows ApplicationmemberMidnightRider31117:53 16 Apr '07  
AnswerRe: USPS Approval For Windows Applicationmembersesmith2k22:41 17 Apr '07  
AnswerRe: USPS Approval For Windows ApplicationmemberMidnightRider3113:55 17 Apr '07  
GeneralRe: USPS Approval For Windows Applicationmembersesmith2k25:05 17 Apr '07  
GeneralRe: USPS Approval For Windows ApplicationmemberMidnightRider3115:42 17 Apr '07  
GeneralRe: USPS Approval For Windows ApplicationmemberBob100387:34 20 Jul '07  
QuestionRe: USPS Approval For Windows Applicationmembersklett19:33 16 Apr '07  
AnswerRe: USPS Approval For Windows ApplicationmemberMidnightRider31119:56 16 Apr '07  
GeneralRe: USPS Approval For Windows Applicationmembersteveklett10:29 17 Apr '07  
GeneralRe: USPS Approval For Windows ApplicationmemberMidnightRider31110:44 17 Apr '07  
GeneralRe: USPS Approval For Windows Applicationmembersklett15:50 18 Apr '07  
AnswerRe: USPS Approval For Windows Applicationmembersesmith2k22:42 17 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 May 2008
Editor:
Copyright 2006 by johnnycantcode
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project