Revision: 38234
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 29, 2010 08:48 by RhinoX64
Initial Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web.Configuration;
using System.Collections.Specialized;
using System.Xml.Linq;
using System.IO;
public static class AddressProvider
{
public static Address StandardizeAddress(Address address)
{
Address addr = new Address();
try
{
//Prepare Request
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
string advserver = "http://production.shippingapis.com/ShippingAPI.dll";
string advuserid = // YOU USER ID HERE
Uri wa = new Uri(advserver);
NameValueCollection nvc = new NameValueCollection();
XElement add = new XElement("Address");
add.SetAttributeValue("ID", "0");
XElement firmname = new XElement("FirmName", string.Empty);
add.Add(firmname);
XElement ad1 = new XElement("Address1", address.Street2);
add.Add(ad1);
XElement ad2 = new XElement("Address2", address.Street1);
add.Add(ad2);
XElement city = new XElement("City", address.City);
add.Add(city);
XElement state = new XElement("State", address.State);
add.Add(state);
XElement zip5 = new XElement("Zip5", address.Zip);
add.Add(zip5);
XElement zip4 = new XElement("Zip4", string.Empty);
add.Add(zip4);
XElement root = new XElement("AddressValidateRequest");
root.SetAttributeValue("USERID", advuserid);
root.Add(add);
nvc.Add("API", "Verify");
nvc.Add("XML", root.ToString());
byte[] data = client.UploadValues(wa, nvc);
MemoryStream ms = new MemoryStream(data);
StreamReader reader = new StreamReader(ms);
string s = reader.ReadToEnd();
//Process Response
XElement response = XElement.Parse(s, LoadOptions.None);
IEnumerable<XElement> error = response.Descendants("Error");
if (error.Count() <= 0)
{
XElement xad = response.Element("Address");
if (xad != null)
{
if (xad.Element("Address1") != null)
{
addr.Street2 = xad.Element("Address1").Value;
}
else
{
addr.Street2 = string.Empty;
}
if (xad.Element("Address2") != null)
{
addr.Street1 = xad.Element("Address2").Value;
}
else
{
addr.Street1 = string.Empty;
}
if (xad.Element("City") != null)
{
addr.City = xad.Element("City").Value;
}
else
{
addr.City = string.Empty;
}
if (xad.Element("State") != null)
{
addr.State = xad.Element("State").Value;
}
else
{
addr.State = string.Empty;
}
if (xad.Element("Zip5") != null)
{
addr.Zip = xad.Element("Zip5").Value;
}
else
{
addr.Zip = string.Empty;
}
}
}
else
{
return null;
}
}
catch
{
}
return addr;
}
public class Address
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
}
Initial URL
Initial Description
requires you to register with USPS. method requires your USPS API key.
Initial Title
USPS Address Correction
Initial Tags
validation
Initial Language
C#