Posted By

RhinoX64 on 12/29/10


Tagged

validation address usps Correction Standardization


Versions (?)

USPS Address Correction


 / Published in: C#
 

requires you to register with USPS. method requires your USPS API key.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.Web.Configuration;
  7. using System.Collections.Specialized;
  8. using System.Xml.Linq;
  9. using System.IO;
  10.  
  11.  
  12. public static class AddressProvider
  13. {
  14. public static Address StandardizeAddress(Address address)
  15. {
  16. Address addr = new Address();
  17. try
  18. {
  19. //Prepare Request
  20. WebClient client = new WebClient();
  21. client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  22. string advserver = "http://production.shippingapis.com/ShippingAPI.dll";
  23. string advuserid = // YOU USER ID HERE
  24. Uri wa = new Uri(advserver);
  25.  
  26. NameValueCollection nvc = new NameValueCollection();
  27.  
  28.  
  29. XElement add = new XElement("Address");
  30. add.SetAttributeValue("ID", "0");
  31.  
  32. XElement firmname = new XElement("FirmName", string.Empty);
  33. add.Add(firmname);
  34.  
  35. XElement ad1 = new XElement("Address1", address.Street2);
  36. add.Add(ad1);
  37.  
  38. XElement ad2 = new XElement("Address2", address.Street1);
  39. add.Add(ad2);
  40.  
  41. XElement city = new XElement("City", address.City);
  42. add.Add(city);
  43.  
  44. XElement state = new XElement("State", address.State);
  45. add.Add(state);
  46.  
  47. XElement zip5 = new XElement("Zip5", address.Zip);
  48. add.Add(zip5);
  49.  
  50. XElement zip4 = new XElement("Zip4", string.Empty);
  51. add.Add(zip4);
  52.  
  53.  
  54. XElement root = new XElement("AddressValidateRequest");
  55. root.SetAttributeValue("USERID", advuserid);
  56. root.Add(add);
  57.  
  58. nvc.Add("API", "Verify");
  59. nvc.Add("XML", root.ToString());
  60.  
  61. byte[] data = client.UploadValues(wa, nvc);
  62. MemoryStream ms = new MemoryStream(data);
  63. StreamReader reader = new StreamReader(ms);
  64. string s = reader.ReadToEnd();
  65. //Process Response
  66.  
  67. XElement response = XElement.Parse(s, LoadOptions.None);
  68. IEnumerable<XElement> error = response.Descendants("Error");
  69.  
  70. if (error.Count() <= 0)
  71. {
  72. XElement xad = response.Element("Address");
  73. if (xad != null)
  74. {
  75. if (xad.Element("Address1") != null)
  76. {
  77. addr.Street2 = xad.Element("Address1").Value;
  78. }
  79. else
  80. {
  81. addr.Street2 = string.Empty;
  82. }
  83. if (xad.Element("Address2") != null)
  84. {
  85. addr.Street1 = xad.Element("Address2").Value;
  86. }
  87. else
  88. {
  89. addr.Street1 = string.Empty;
  90. }
  91. if (xad.Element("City") != null)
  92. {
  93. addr.City = xad.Element("City").Value;
  94. }
  95. else
  96. {
  97. addr.City = string.Empty;
  98. }
  99. if (xad.Element("State") != null)
  100. {
  101. addr.State = xad.Element("State").Value;
  102. }
  103. else
  104. {
  105. addr.State = string.Empty;
  106. }
  107. if (xad.Element("Zip5") != null)
  108. {
  109. addr.Zip = xad.Element("Zip5").Value;
  110. }
  111. else
  112. {
  113. addr.Zip = string.Empty;
  114. }
  115.  
  116. }
  117. }
  118. else
  119. {
  120. return null;
  121. }
  122. }
  123. catch
  124. {
  125.  
  126. }
  127.  
  128. return addr;
  129. }
  130.  
  131. public class Address
  132. {
  133. public string Street1 { get; set; }
  134. public string Street2 { get; set; }
  135. public string City { get; set; }
  136. public string State { get; set; }
  137. public string Zip { get; set; }
  138. }
  139.  
  140. }

Report this snippet  

You need to login to post a comment.