locked
await httpClient.GetAsync(_URL)) Hangs RRS feed

  • Question

  • User-868767480 posted

    Evening All,

    I'm new to ASP I've put this together but it just hangs here ("await httpClient.GetAsync(_URL))" not sure why can anybody help.

    Thanks

    Madaxe

    namespace ImplementationProject.ImplementationMain
    {
        public class WeatherAppMainImplementation
        {
            static HttpClient client = new HttpClient();
    
            private static string _APIKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            private static string _BaseURI = "https://api.openweathermap.org/data/2.5/";
            private static string _URL = "weather?q=Sydney,{{country}}/";
    
            public static void GetWeather()
            {
                try
                {
                    Task<RootObject> DeviceTask = WeatherAppMainImplementation.HttpGetWeather();
                    DeviceTask.Wait();
    
                    if (DeviceTask.Result != null)
                    {
                        MessageBox.Show("hi");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private async static Task<RootObject> HttpGetWeather()
            {
                RootObject ReturnWeather = null;
                try
                {
                    using (HttpClient httpClient = new HttpClient())
                    {
                        httpClient.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
                        httpClient.DefaultRequestHeaders.Add("X-API-KEY-TOKEN", WeatherAppMainImplementation._APIKey);
    
                        httpClient.BaseAddress = new Uri(WeatherAppMainImplementation._BaseURI);
    
                        using (HttpResponseMessage HttpResponseMessage = await httpClient.GetAsync(_URL))
                        {
                            if (HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                            {
                                using (HttpContent HttpContent = HttpResponseMessage.Content)
                                {
                                    string MyContent = await HttpContent.ReadAsStringAsync();
                                    ReturnWeather = JsonConvert.DeserializeObject<RootObject>(MyContent);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return ReturnWeather;
            }
        }
    }

    Wednesday, March 11, 2020 8:21 PM

All replies

  • User-868767480 posted

    i changed the Httpresponce to

    HttpResponseMessage response = await client.GetAsync(_URL, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

    Which results in this

    {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Connection: keep-alive
    X-Cache-Key: /data/2.5/weather?q=sydney,%7b%7bcountry%7d%7d
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    Access-Control-Allow-Methods: GET, POST
    Date: Wed, 11 Mar 2020 20:42:29 GMT
    Server: openresty
    Content-Length: 107
    Content-Type: application/json; charset=utf-8
    }}

    Wednesday, March 11, 2020 8:46 PM
  • User-868767480 posted

    so if i simplify remove the base url etc i get a good result why? does the API key have to be in the url directly?

                    string myurl = "https://api.openweathermap.org/data/2.5/weather?q=Sydney,{{country}}&APPID=xxxxxxxxxxxxxxxxxxxxxxxx";
                    //GET Method  
                    HttpResponseMessage response = await client.GetAsync(myurl, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);

    Wednesday, March 11, 2020 8:52 PM
  • User409696431 posted

    Look at the documentation for the API you are using.  Specifically, https://openweathermap.org/appid

    Example API call:
    http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID={APIKEY}
    Parameters:
    APPID {APIKEY} is your unique API key
    

    Wednesday, March 11, 2020 11:44 PM