How to validate the given address using USPS?

前端 未结 4 1061
囚心锁ツ
囚心锁ツ 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.

提交回复
热议问题