public static ZipCodeLookupResponse LookupZipCode(string firmName,string address1,string address2,string city,string state) { // ZipCodeLookupResponse response = new ZipCodeLookupResponse(); USPSWebToolsClient client = new USPSWebToolsClient(); try { DataSet ds = client.LookupZipCode(firmName,address1,address2,city,state); if (ds != null) response.Merge(ds); client.Close(); } catch (TimeoutException te) { client.Abort(); throw new ApplicationException(te.Message); } catch (FaultException<USPSFault> cfe) { client.Abort(); throw new ApplicationException(cfe.Detail.Message); } catch (FaultException fe) { client.Abort(); throw new ApplicationException(fe.Message); } catch (CommunicationException ce) { client.Abort(); throw new ApplicationException(ce.Message); } return response; }
internal static async Task <List <Address> > ZipCodeLookupAsync(List <Address> input) { // limit is 5 addresses per request string requestGuid = Guid.NewGuid().ToString(); Log.Information("{area}: New request for {packageTotal} packages. {requestGuid}", "ZipCodeLookup()", input.Count, requestGuid); List <Address> output = new(); ZipCodeLookupRequest request; int index = 0; while (index < input.Count) { request = new ZipCodeLookupRequest { Address = input.Skip(index).Take(5).ToList(), USERID = UspsApiUsername, }; Log.Information("{area}: Fetching rates for {packageCount} package(s). {requestGuid}", "ZipCodeLookup()", input.Count, requestGuid); XmlSerializer xsSubmit = new(typeof(ZipCodeLookupRequest)); var xml = ""; using (var sww = new StringWriter()) { using XmlWriter writer = XmlWriter.Create(sww); xsSubmit.Serialize(writer, request); xml = sww.ToString(); } string uspsUrl = "https://secure.shippingapis.com/ShippingAPI.dll"; var formData = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("API", "ZipCodeLookup"), new KeyValuePair <string, string>("XML", xml) }); HttpClient httpClient = new() { Timeout = TimeSpan.FromSeconds(50) }; HttpResponseMessage response = null; int retryCount = 0; DateTime responseTimer = DateTime.Now; while (response == null || response.StatusCode != System.Net.HttpStatusCode.OK) { if (retryCount > 0) { Log.Warning("{area}: USPS Failed to Respond after " + retryCount + " seconds. Attempt {retryCount}. {requestGuid}", "ZipCodeLookup()", retryCount, requestGuid); } response = await httpClient.PostAsync(uspsUrl, formData).ConfigureAwait(false); Thread.Sleep(1000 * retryCount); httpClient.CancelPendingRequests(); retryCount++; if (retryCount > 50) { Log.Error("{area}: USPS Failed to Respond after 50 attempts. {requestGuid}", "ZipCodeLookup()", retryCount, requestGuid); throw new UspsApiException("408: After many attempts, the request to the USPS API did not recieve a response. Please try again later."); } } TimeSpan responseTime = DateTime.Now.TimeOfDay.Subtract(responseTimer.TimeOfDay); var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); Log.Information("{area}: USPS response received in {responseTime} ms. {requestGuid}", "ZipCodeLookup()", responseTime.Milliseconds, requestGuid); try { XmlSerializer deserializer = new(typeof(ZipCodeLookupResponse)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); ZipCodeLookupResponse responseJson = (ZipCodeLookupResponse)deserializer.Deserialize(ms); index += 5; foreach (Address validatedAddress in responseJson.Address) { Address orig = input.First(i => i.ID == validatedAddress.ID); if (validatedAddress.Error != null) { Log.Warning("{area}: USPS Returned Error: {uspsErrorNumber} {uspsErrorDescription} {requestGuid}", "ZipCodeLookup()", validatedAddress.Error.Number, validatedAddress.Error.Description, requestGuid); orig.Error = validatedAddress.Error; } orig.Zip5 = validatedAddress.Zip5; orig.Zip4 = validatedAddress.Zip4; output.Add(orig); } } catch (Exception ex) { Log.Error("{area}: Exception: {ex} {requestGuid}", "ZipCodeLookup()", ex.ToString(), requestGuid); throw new UspsApiException(ex); } } if (output.Count != input.Count) { // something went wrong because counts should always match Console.WriteLine("Counts did not match between input and output"); Log.Error("{area}: Counts did not match between input and output. {requestGuid}", "ZipCodeLookup()", requestGuid); throw new UspsApiException("Counts did not match between input and output"); } return(output); }