Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

REST Api GetAsync method : Control doesnt comeback

$
0
0

Dear all,

I am trying a sample asp.net core 1.1 webapp and trying to pull weather data from Openweathermap", but the control doesn't come back from GetAsync method!! nor the error I get. i use asp.netcore 1.1 engine with visual studio 2017. here is the code snippet,

client.BaseAddress = new Uri("http://api.openweathermap.org");

var url = String.Format("$/data/2.5/weather?id=2172797&appid=mykey");

var response = await client.GetAsync(url); 
-----

 using (var client = new HttpClient())
            {
               try
               {
                    client.BaseAddress = new Uri("http://api.openweathermap.org");

                var url = String.Format("$/data/2.5/weather?q=london&appid=xxmykeyxx"); var response = await client.GetAsync(url);  ///control struck here, control hungs here...

                response.EnsureSuccessStatusCode();

                    var stringResult = await response.Content.ReadAsStringAsync();
                    var rawWeather = JsonConvert.DeserializeObject<OpenWeatherResponse>(stringResult);
                    return Ok(new
                    {
                        Temp = rawWeather.Main.Temp,
                        Summary = string.Join(",", rawWeather.Weather.Select(x => x.Main)),
                        City = rawWeather.Name
                    });

                }
                catch (HttpRequestException httpRequestException)
                {
                  return BadRequest($"Error getting weather from OpenWeather: {httpRequestException.Message}");
                }
            }

when i tested with postman-tool, i was able to receive weather information. 

I tried adding authorization headers as well , but no luck so far...:(

var byteArray = Encoding.ASCII.GetBytes("craghuramanace:guhan_123");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

Please help me when i went wrong.

Thanks

Elisa adhibala
P.S. I have embdeded source for any clue.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>