Click here to Skip to main content
15,884,537 members
Articles / Web Development / ASP.NET

UPS Address Validation Sample With ASP.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.06/5 (9 votes)
26 Oct 2007CPOL 121.1K   3.8K   28   18
A UPS Online Tool address validation sample with ASP.NET and C#.

Screen Shot

Introduction

I'm right now working on UPS online tool integration with my e-commerce web site. It is quite difficult to find sample source code for use with .NET. I created this article for .NET users who want to integrate their e-commerce web sites with the UPS Online Tool.

See details at UPS Online Tool.

Using the code

To use UPS address validation, you have to register a UPS account at UPS Account Register and request for a UPS access key at Get UPS Access Key.

To run my sample website, you have to replace your UPS account for user ID, password, and access key in the web.config.

XML
<!-- Web.Config -->
<add key="AccessLicenseNumber" value="xxxxxxxxxxxxxxxx"/>
<add key="UserId" value="xxxxxx"/>
<add key="Password" value="xxxxxx"/>

This is the main code for the request address validation with the UPS online tool service.

C#
// Create Request to UPS online tool address validation service
ASCIIEncoding encodedData = new ASCIIEncoding();
byte[] byteArray = encodedData.GetBytes(requestText);

// open up da site
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = "POST";
wr.KeepAlive = false;
wr.UserAgent = "Benz";
wr.ContentType = "application/x-www-form-urlencoded";
wr.ContentLength = byteArray.Length;
try
{
    // send xml data
    Stream SendStream = wr.GetRequestStream();
    SendStream.Write(byteArray, 0, byteArray.Length);
    SendStream.Close();
    // get da response
    HttpWebResponse WebResp = (HttpWebResponse)wr.GetResponse();

    using (StreamReader sr = new StreamReader(WebResp.GetResponseStream()))
    {
        result = sr.ReadToEnd();
        sr.Close();
    } 

    WebResp.Close();
}
catch (Exception ex)
{
    // Unhandle exception occure
    result = ex.Message;
}

License

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


Written By
Thailand Thailand
.NET developer

Comments and Discussions

 
QuestionNo Access Identification provided Pin
Alexander B. Deyto1-Nov-11 21:59
Alexander B. Deyto1-Nov-11 21:59 
GeneralMy vote of 5 Pin
DougVarga6-Nov-10 15:49
DougVarga6-Nov-10 15:49 
GeneralUnable to connect to the remote server Pin
rishi_narula17-Feb-09 0:10
rishi_narula17-Feb-09 0:10 
GeneralRe: Unable to connect to the remote server Pin
Member 379819910-Nov-09 7:07
Member 379819910-Nov-09 7:07 
QuestionHow to get the shipping method form ups by different zip code? Pin
Member 378325711-Aug-08 8:23
Member 378325711-Aug-08 8:23 
GeneralCalculate Shipping rates for canda postal code Pin
Ayesha rana27-Jul-08 23:05
Ayesha rana27-Jul-08 23:05 
QuestionUsing the address field? Pin
SuperJason2k6-Mar-08 7:45
SuperJason2k6-Mar-08 7:45 
AnswerRe: Using the address field? Pin
Beaniiman20-May-08 9:30
Beaniiman20-May-08 9:30 
GeneralRe: Using the address field? Pin
Member 268641314-Sep-10 9:45
Member 268641314-Sep-10 9:45 
GeneralUPS online tools Pin
Ron Yam8-Jan-08 10:08
Ron Yam8-Jan-08 10:08 
General[Message Deleted] Pin
sara5528-Jul-08 10:46
sara5528-Jul-08 10:46 
GeneralRe: UPS online tools Pin
sara5530-Jul-08 6:33
sara5530-Jul-08 6:33 
GeneralRe: UPS online tools Pin
sara5528-Jul-08 12:25
sara5528-Jul-08 12:25 
QuestionUPS Shipping Tool Pin
Gfw26-Oct-07 1:38
Gfw26-Oct-07 1:38 
AnswerRe: UPS Shipping Tool Pin
Gevorg26-Oct-07 5:48
Gevorg26-Oct-07 5:48 
GeneralRe: UPS Shipping Tool Pin
Gfw27-Oct-07 23:54
Gfw27-Oct-07 23:54 
AnswerRe: UPS Shipping Tool Pin
Benz CPE28-Oct-07 17:18
Benz CPE28-Oct-07 17:18 
GeneralRe: UPS Shipping Tool Pin
Gfw30-Oct-07 6:00
Gfw30-Oct-07 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.