Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am trying to fetch some JSON from OpenWeatherMap, but i do not get anything returned. The URL i am trying to get JSON from is this: http://api.openweathermap.org/data/2.5/weather?lat=54.941027&lon=8.848902&units=metric&APPID=myAPIkey
But if i use another url like umbraco.com it returns the HTML for the page.
My code looks like this:
var url2 = ("http://www.umbraco.com"); var response = ""; var requestWeather = (HttpWebRequest)WebRequest.Create(url2); if((@requestWeather.GetResponse().ContentLength > 0)){ var stream = new StreamReader(requestWeather.GetResponse().GetResponseStream()); response = stream.ReadToEnd(); if(stream != null){ stream.Close(); } }
Can any of you guys see the problem?
Is there a smarter way of doing this? If so, how could it be done?
Hi,
try this:
var url = "http://api.openweathermap.org/data/2.5/weather?lat=54.941027&lon=8.848902&units=metric&APPID=myAPIkey";
WebClient client = new WebClient();
string reply = client.DownloadString(url);
Regards,
moshe
Also, if you want to have more control on your request you can use the following:
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string res = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
res = streamReader.ReadToEnd();
}
Thank you Moshe! Didn't think it was that simple!
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Getting JSON from an external URL
I am trying to fetch some JSON from OpenWeatherMap, but i do not get anything returned. The URL i am trying to get JSON from is this: http://api.openweathermap.org/data/2.5/weather?lat=54.941027&lon=8.848902&units=metric&APPID=myAPIkey
But if i use another url like umbraco.com it returns the HTML for the page.
My code looks like this:
Can any of you guys see the problem?
Is there a smarter way of doing this? If so, how could it be done?
Hi,
try this:
var url = "http://api.openweathermap.org/data/2.5/weather?lat=54.941027&lon=8.848902&units=metric&APPID=myAPIkey";
WebClient client = new WebClient();
string reply = client.DownloadString(url);
Regards,
moshe
Also, if you want to have more control on your request you can use the following:
var url = "http://api.openweathermap.org/data/2.5/weather?lat=54.941027&lon=8.848902&units=metric&APPID=myAPIkey";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
string res = "";
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
res = streamReader.ReadToEnd();
}
Thank you Moshe! Didn't think it was that simple!
is working on a reply...