Uncategorized

[C#] HTTP GET Request

Below is an example of get request sending to OpenWeatherMap API:

var url = $"http://api.openweathermap.org/data/2.5/weather?q={keyword}&appid={Configuration.OpenWeatherMapId}&units=metric";
 
            var request = (HttpWebRequest) WebRequest.Create(url);
 
            var response = (HttpWebResponse) request.GetResponse();
 
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                var json = sr.ReadToEnd();
 
                var result = JsonConvert.DeserializeObject(json);
 
                return result;
            }

Note: We need to install nuget Newton.Json

In order to parse Json object to C# object, after copy the Json data: In Visual Studio > Edit > Paste Special > Paste JSON As Classes.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s