Problems when searching for zip code from another country

Robson Amaral 1 Reputation point
2021-06-02T16:10:57+00:00

I have the following code that when I type the postal code it returns with the data of my country, but I would like to do a search using the postal code of other countries. How can I adapt my structure to my code?

public ActionResult Pesquisar(string zipCode)
{

        string key = ConfigurationManager.AppSettings.Get("keyAPIGoogleCloudServices");
        ReplyZipCodeGoogle ReplyZipCodeGoogle = null;
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("https://maps.google.com/maps/api/geocode/");
            //HTTP GET
            var parameters = "&sensor=false&languague=pt-BR&region=br";
            var key = "&key=" + key;
            var research = "json?address=" + zipCode;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3170;
            //var responseTask = client.GetAsync("json?address=29102-100&sensor=false&languague=pt-BR&region=br&key=AIzaSyDPFP6WqSoSiECqAJqE1P3kVkueJAujVtQ");
            var responseTask = client.GetAsync(pesquisa + parameters + key);
            responseTask.Wait();

            var result = responseTask.Result;
            if (result.IsSuccessStatusCode)
            {
                var readTask = result.Content.ReadAsAsync<ReplyZipCodeGoogle>();
                readTask.Wait();
                ReplyZipCodeGoogle = readTask.Result;

            }
            else //web api sent error response 
            {

            }
        }
        if(RespostaCepGoogle.Status == "ZERO_RESULTS")
            throw new CoreException("Zip code does not exist. Please enter a valid zip code.");

}

How can I adapt a way to get the zip code from Paraguay, for example?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-06-03T02:39:15.63+00:00

    Since I don't know which nuget package you are using, I cannot reproduce your problem now.

    But according to your description, I wrote some code using bing map API, please try to see if it can meet your needs.

           static async Task Main(string[] args)  
            {  
                string zipCode = "99999";  
               await Pesquisar(zipCode);  
                Console.WriteLine("Press any key to continue......");  
                Console.ReadLine();  
            }  
            public static async Task Pesquisar(string zipCode)  
            {  
                HttpClient httpClient = new HttpClient();  
      
                // get key from https://www.microsoft.com/en-us/maps/create-a-bing-maps-key#basic   
                string mapKey = @"your map key";  
                try  
                {  
                    string path = String.Format(@"http://dev.virtualearth.net/REST/v1/Locations?postalCode={0}&key={1}", zipCode, mapKey);  
                    HttpResponseMessage response = await httpClient.GetAsync(path);  
                    response.EnsureSuccessStatusCode();  
                    string responseBody = await response.Content.ReadAsStringAsync();  
      
                    Console.WriteLine(responseBody);  
                }  
                catch (HttpRequestException e)  
                {  
                    Console.WriteLine("\nException Caught!");  
                    Console.WriteLine("Message :{0} ", e.Message);  
                }  
            }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.