title: No such host is known.(calling weatherAPI from webapi controller using HttpClient)
body: <p>I am trying to call the openWeatherApi from asp.net WebAPI. When I receive the name of a city from front-end, I use that to lookup the city/airport from the database. Then, I use the lat and long of the airport to call the openweather API. I want to use the response to make further calculations and predictions.</p>
  
  <p>My code is:</p>
  
  <pre><code>HttpClient client = new HttpClient();
  
  try
                  {
                      client.BaseAddress = new Uri("http://api.openweather.org");
                       var response = await client.GetAsync($"/data/2.5/weather?lat={airport.AirportLatitude}&amp;lon={airport.AirportLongitude}&amp;appid={apiKey}");
                    //  var response = await // client.GetAsync($"/data/2.5/weather?q=London,uk&amp;appid={apiKey}");
                      response.EnsureSuccessStatusCode();
  
                      var stringResult = await response.Content.ReadAsStringAsync();
                      var rawWeather = JsonConvert.DeserializeObject&lt;OpenWeatherResponse&gt;(stringResult);
                      System.Console.WriteLine("$$$$$$$$$$$After API call made$$$$$$$$$$$$$$$");
                      System.Console.WriteLine(response);
                      return Ok(new {
                          Temp = rawWeather.Main.Temp,
                          Summary = string.Join(",", rawWeather.Weather.Select(x=&gt;x.Main)),
                          City = rawWeather.Name
                      });
                      // return Ok(response);
                  }
  
                  catch(HttpRequestException httpRequestException)
                  {   
                      System.Console.WriteLine(httpRequestException.Message); 
                      return BadRequest($"Error getting weather : {httpRequestException.Message}");
                  }
  </code></pre>
  
  <p>When I run this app, sometimes I get 400BadRequest, and other times I get 500:Internal Server Error</p>
  
  <p>1) No such host is known.
  2) System.InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request.</p>
  
  <p>Startup.cs</p>
  
  <pre><code>services.AddTransient&lt;HttpClient&gt;(); 
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  </code></pre>
  
  <p>When I try to put the complete openweather url in my browser, it returns json, but it gives error when trying to run from ASP.NET CORE Web Api Controller.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>Thanks in advance</p>