无法通过dotnet代码进行休息呼叫

时间:2016-10-21 11:49:03

标签: c# rest

我想致电http://api.worldweatheronline.com/free/v1/weather.ashx?q=28078&format=json&num_of_days=1&key=<key>

将在我的.NET项目中返回JSON响应。

如果我在Chrome或Fiddler或PostMan中浏览此网址,我可以在测试框中获得响应,但是通过我的代码,它无法在同一个测试框中工作。

我收到以下错误

@"System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 31.193.9.233:80
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()

以下是我的dotnet代码

 StringBuilder st = new StringBuilder();
        string response1 = "";

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.worldweatheronline.com/free/v1/weather.ashx?q=28078&format=json&num_of_days=1&key=<key>");
                request.Method = "GET";
                request.ContentType = "application/json";
                request.Proxy = WebRequest.GetSystemWebProxy();
                try
                {
                    WebResponse webResponse = request.GetResponse();
                    using (Stream webStream = webResponse.GetResponseStream())
                    {
                        if (webStream != null)
                        {
                            using (StreamReader responseReader = new StreamReader(webStream))
                            {
                                 response1 = responseReader.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    response1 = e.ToString();
                }
            }
            catch (Exception ex)
            {
                response1 = ex.ToString(); 
            }

            st.Append("2");
            return response1;

从我的开发框中,dotnet代码也能得到响应。 只有测试盒才有响应。

我认为这与机器设置更相关,但没有太多想法。

请帮忙。

0 个答案:

没有答案