Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在asp.net中读取api结果_C#_Asp.net_Json_Api - Fatal编程技术网

C# 在asp.net中读取api结果

C# 在asp.net中读取api结果,c#,asp.net,json,api,C#,Asp.net,Json,Api,我使用下面的C#/ASP.net代码从api获取天气数据。如果我将api链接复制/粘贴到浏览器上,并返回所需的所有数据,则该api链接将起作用。 但在我的C#代码中,出现错误“{”无法连接到远程服务器“}” 有人知道怎么回事吗 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System

我使用下面的C#/ASP.net代码从api获取天气数据。如果我将api链接复制/粘贴到浏览器上,并返回所需的所有数据,则该api链接将起作用。 但在我的C#代码中,出现错误“{”无法连接到远程服务器“}”

有人知道怎么回事吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
using System.Runtime.Serialization;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml;

namespace WebApplication2
{
    public partial class GetData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [System.Web.Services.WebMethod]
        public static  void getData()
        {


            var url = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=e67fab67a2bc61c221e8a6165965c107";
            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            var response = client.GetAsync(url).Result;

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            getData();
        }  
    }

我不确定为什么您无法连接远程服务器可能是因为关闭了防火墙解决了您的问题。试试下面的代码。


我不确定为什么您无法连接远程服务器可能是因为关闭了防火墙解决了您的问题。试试下面的代码。


我注意到问题是,我的internet连接使用的是内部dns服务器,由于安全限制,该服务器的设置不允许此类呼叫。在我们办公室外面,代码运行得很好

我注意到问题是,我的internet连接使用的是内部dns服务器,由于安全限制,该服务器的设置不允许此类呼叫。在我们办公室外面,代码运行得很好

string urlAddress = "http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=e67fab67a2bc61c221e8a6165965c107";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
  Stream receiveStream = response.GetResponseStream();
  StreamReader readStream = null;

  if (response.CharacterSet == null)
  {
     readStream = new StreamReader(receiveStream);
  }
  else
  {
     readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
  }

  string data = readStream.ReadToEnd();

  response.Close();
  readStream.Close();
}