Pergunta

I am currently trying to use the United States Postal Service's Web Tools API to validate a user's address. However, no matter what combination of information I use or technique to connect to the web service, I get the same exception:

Unable to connect to the remote service

With the same Inner Exception

A connection attempt failed because the connected party did not properly respond after 
a period of time, or established connection failed because connected host has failed to 
respond 56.0.34.43:443

I am using a C# WebClient object for the first time, however. Can anyone see something I'm doing wrong here?

    private const string USPS_USERID = "xxxxxxx";
    private const string BASEURL = "https://secure.shippingapis.com/ShippingAPITest.dll";

        try
        {
            string USPS = BASEURL + "?API=Verify&XML=<AddressValidateRequest USERID=\"" + USPS_USERID + "\">";
            USPS += "<Address ID='0'>";
            USPS += "<Address1>" + addressInfo[0] + "</Address1>";
            USPS += "<Address2>" + addressInfo[1] + "</Address2>";
            USPS += "<City>" + addressInfo[2] + "</City>";
            USPS += "<State>" + addressInfo[3] + "</State>";
            USPS += "<Zip5>" + addressInfo[4] + "</Zip5>";
            USPS += "<Zip4></Zip4>";
            USPS += "</Address></AddressValidateRequest>";

            WebClient wsClient = new WebClient();

            byte[] responseData = wsClient.DownloadData(USPS);

            string response = string.Empty;

            foreach (byte item in responseData)
            {
                response += (char) item;
            }

            return Json(response, JsonRequestBehavior.AllowGet);

        }
        catch (Exception ex)
        {
            return null;
        }
Foi útil?

Solução

In the case of mailing addresses (in the United States) the USPS is certainly the authority. However, I have heard from many peers that their API is a little bit lacking. Because the USPS API offers no SLA (Service Level Agreement) they are able to perform routine or non-routine maintenance at any time. So, if you absolutely must use the USPS API, then you're stuck with whatever they give you and whatever downtime they have.

If you have the option to consider another address validation API service, a quick google search for "address validation API" will show you some of the best ones. You have options that range from free to very expensive.

So, while I can't speak for why the USPS API is down, hopefully I can steer you to a more reliable service. I've heard good things about easypost.com, it's a free service. You've also got Texas A&M University which has a free address verification service. So, whatever service you find, make sure you look for one (free or paid) that actually has an SLA with a guaranteed uptime that will provide what you need.

I've been working at SmartyStreets for almost three years and so I know a thing or two about address validation.

Outras dicas

USPS site is down! So they are probably having issues with their servers.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top