mercredi 3 avril 2019

Web request CSharp

I have a C# code that consumes a web service data from an external site. it was working and displaying the weather data on my page. but Now it throws with "An exception occurred during a WebClient request" error. Been trying to research this but no luck.

protected void GetWeatherInfo(string url) {

        string url1 = string.Format(url);
        using (WebClient client = new WebClient())
        {
            WebProxy proxyObj = new WebProxy(url1);

            proxyObj.Credentials = CredentialCache.DefaultCredentials;


            client.Proxy = proxyObj;
            try
            {
                string json = client.DownloadString(url1);

                WeatherInfo weatherInfo = (new JavaScriptSerializer()).Deserialize<WeatherInfo>(json);
                lblCity_Country.Text = weatherInfo.city.name + "," + weatherInfo.city.country;
                imgCountryFlag.ImageUrl = string.Format("http://openweathermap.org/images/flags/{0}.png", weatherInfo.city.country.ToLower());
                lblDescription.Text = weatherInfo.list[0].weather[0].description;
                imgWeatherIcon.ImageUrl = string.Format("http://openweathermap.org/img/w/{0}.png", weatherInfo.list[0].weather[0].icon);
                lblTempMin.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.min, 1));
                lblTempMax.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.max, 1));
                lblTempDay.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.day, 1));
                lblTempNight.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.night, 1));
                lblHumidity.Text = weatherInfo.list[0].humidity.ToString();
                tblWeather.Visible = true;
            }
            catch (Exception ex)
            {
                string excep = ex.Message;
            }

        }
    }




Aucun commentaire:

Enregistrer un commentaire