远程服务器返回错误:(400)错误的请求。 apiRequest.ChallengedUri'引发了类型为'System.NullReferenceException'的异常

时间:2019-02-01 10:55:13

标签: asp.net-mvc-4 openweathermap

我正在尝试从网址中获取响应,该网址必须是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'的异常

0 个答案:

没有答案