question

RobsonAmaral-0530 avatar image
0 Votes"
RobsonAmaral-0530 asked TimonYang-MSFT commented

Problems when searching for zip code from another country

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?

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RobsonAmaral-0530
May I know if you have got any chance to check my answer? If there are still some issues with this code, please let me know and we can continue to improve it.

0 Votes 0 ·

1 Answer

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.