How to validate the given address using USPS?

前端 未结 4 1055
囚心锁ツ
囚心锁ツ 2021-02-01 23:29

I want to validate the given address (address, city, state, zip) to the USPS and return back the result if the provided address is a valid address. and if it is not the valid ad

相关标签:
4条回答
  • 2021-02-01 23:57

    From here

    ///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 validated by the
    ///USPS servers
    Address validatedAddress = m.ValidateAddress(a);
    

    NOTE: For some reason, you need to put the actual Address as Address2. If you attempt to put Address1 as "6406 Ivy Lane" it will fail. Address1 is apparently for apartment or suite number. Courtesy Simon Weaver's comment below.

    0 讨论(0)
  • 2021-02-02 00:06

    The United States Postal Service (USPS) does offer this service via their Address Information APIs:

    • USPS: Ecommerce APIs

    Here's a Code Project article/library how you could use this services in .NET:

    • United States Postal Service (USPS) Web Tools Wrapper

    Note

    • Before you can use this library, you will need to get a USPS Web Tools ID by filling out this form.

    • To request the Address Informational APIs (Verify etc.) you need additional permissions. Also fill out this form to apply for these permissions.

    0 讨论(0)
  • 2021-02-02 00:12

    The Service Objects address validation web service can determine if an address is valid according to the USPS. Below are a couple of C# examples (RESTful request and SOAP request):

    Restful request

    string mainURL = "https://trial.serviceobjects.com/AV3/api.svc/GetBestMatchesJson/" + businessName + "/" + address + "/" + address2 + "/" + city + "/" + state + "/" + zip + "/" + licenseKey;
    AV3Response result = null;
    
    HttpWebRequest request = WebRequest.Create(mainURL ) as HttpWebRequest;
    request.Timeout = 5000;//timeout for get operation
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
     if (response.StatusCode != HttpStatusCode.OK)
     throw new Exception(String.Format(
     "Server error (HTTP {0}: {1}).",
     response.StatusCode,
     response.StatusDescription));
     //parse response
     DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(AV3Response));
     object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
     result = objResponse as AV3Response;
    //processing result
    if (result.error == null)
    {
     //process result
    }
    else
    {
     //process error
    }
    

    The other option is to consume the wsdl and make a SOAP request.

    SOAP request

    //Add a service to your application https://trial.serviceobjects.com/av3/api.svc/
    AV3Client_Primary = new AddressValidation3Client("DOTSAddressValidation3");
    response = AV3Client_Primary.GetBestMatches(Business, Address, Address2, City, State, PostalCode, licenseKey);
    

    The response field will contain information about the validity of the address. The DPV score can be used to determine if an address is deliverable, non deliverable, or is close to correct but missing some vital piece of information (apt, ste, rr #).

    DPV Codes

    If you'd like more in depth information on the service's outputs you can check out here

    0 讨论(0)
  • 2021-02-02 00:17

    If I may chime in here -- I used to work in the address validation industry for SmartyStreets, which is a CASS-Certified vendor of these services.

    Note first that, while the USPS is the authority on address data, their forte is NOT maintaining an API and providing support. Also, be sure to note the agreement which you signed:

    • User agrees to use the USPS Web site, APIs and USPS data to facilitate USPS shipping transactions only.

    So unless you're mailing or shipping with the USPS by the use of their API, it's not agreeable to use the API at all. This among the other problems you're having is grounds to look for a better solution -- if I was you.

    Anyway, there are actually quite a few out there. I'll let you do your own research, but of course I would suggest one that I've worked on called LiveAddress. It's free, and returns more data and is more reliable than the USPS' API.

    Update: Here's some C# code examples on GitHub that will probably be useful.

    0 讨论(0)
提交回复
热议问题