我使用下面的C#/ ASP.net代碼從api獲取天氣數據。如果我將api鏈接複製/粘貼到瀏覽器上,則返回所有我想要的數據。 但是在我的C#代碼中,我得到錯誤「{」無法連接到遠程服務器「}」在asp.net中讀取api結果
任何人都知道什麼是錯的?
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();
}
}