/ Published in: C#
requires you to register with USPS.
method requires your USPS API key.
method requires your USPS API key.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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) { try { //Prepare Request 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 add.SetAttributeValue("ID", "0"); add.Add(firmname); add.Add(ad1); add.Add(ad2); add.Add(city); add.Add(state); add.Add(zip5); add.Add(zip4); root.SetAttributeValue("USERID", advuserid); root.Add(add); nvc.Add("API", "Verify"); nvc.Add("XML", root.ToString()); byte[] data = client.UploadValues(wa, nvc); 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; } } }