我正在尝试从网址中获取响应,该网址必须是JSON,如下所示:
我使用此代码
if (cities != null)
{
/*Calling API http://openweathermap.org/api */
string apiKey = "xxxxxx";
HttpWebRequest apiRequest =
WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?id=" +
cities + "&appid=" + apiKey + "&units=metric") as HttpWebRequest;
string apiResponse = "";
using (HttpWebResponse response = (HttpWebResponse)apiRequest.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
apiResponse = reader.ReadToEnd();
}
/*End*/
所以我在HttpWebResponse response =(HttpWebResponse)apiRequest.GetResponse()作为HttpWebResponse时遇到错误
ChallengedUri ='apiRequest.ChallengedUri'引发了类型为'System.NullReferenceException'的异常