天气 API http://www.weather.gov/
Weather API http://www.weather.gov/
我一直在通过访问他们的 XML 文件从国家气象局获取天气信息。但截至今天,我一直收到访问被拒绝错误 (403) 我的服务器被阻止了吗?如果可以,在美国我可以使用什么来免费获取天气信息?
我不敢相信我的 Web 服务只被几次点击就被阻止了。以防万一这是我用来测试天气数据的计划作业:
public override async Task ExecuteAsync()
{
GoogleGeocoder geocoder;
//Geocode variables
string apiKey = WebConfigurationManager.AppSettings["googleApiKey"];
if (String.IsNullOrEmpty(apiKey))
{
geocoder = new GoogleGeocoder();
}
else
{
geocoder = new GoogleGeocoder(apiKey);
}
string longitude = string.Empty;
string latitude = string.Empty;
var xdoc = new XDocument();
var project = Project();
//Query for each project and get their longitude and latitude
DataTable dataTable = SqlHelper.ExecuteDataset("getAll", "1").Tables[0];
if (dataTable.Rows.Count > 0) {
foreach (DataRow dataRow in dataTable.Rows) {
Map.DataToObject(dataRow, project);
//Find Longitude and latitude based on zipcode or address.
IEnumerable<Address> addresses = geocoder.Geocode(project.SiteCity + "," + project.SiteState + " " + project.SitePostalCode);
longitude = addresses.First().Coordinates.Latitude.ToString();
latitude = addresses.First().Coordinates.Longitude.ToString();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://forecast.weather.gov/MapClick.php");
string uri = "http://forecast.weather.gov/MapClick.php?lat=" + latitude + "&lon=" + longitude + "&FcstType=dwml";
HttpResponseMessage response = await client.GetAsync(uri);
xdoc = XDocument.Parse(await response.Content.ReadAsStringAsync(), LoadOptions.None);
Services.Log.Info(xdoc.Descendants("wordedForecast").Descendants("text").ElementAt(0).Value);
//Update or create an weather entry for each project
}
}
return;//Task.FromResult(true);
}
}
您的网站似乎更改了政策。它需要设置User-Agent
header。您只需将其设置为某个值即可。
var url = "http://forecast.weather.gov/MapClick.php?lat=42&lon=-75&FcstType=dwml";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Whosebug/1.0");
var xml = await client.GetStringAsync(url);
我一直在通过访问他们的 XML 文件从国家气象局获取天气信息。但截至今天,我一直收到访问被拒绝错误 (403) 我的服务器被阻止了吗?如果可以,在美国我可以使用什么来免费获取天气信息?
我不敢相信我的 Web 服务只被几次点击就被阻止了。以防万一这是我用来测试天气数据的计划作业:
public override async Task ExecuteAsync()
{
GoogleGeocoder geocoder;
//Geocode variables
string apiKey = WebConfigurationManager.AppSettings["googleApiKey"];
if (String.IsNullOrEmpty(apiKey))
{
geocoder = new GoogleGeocoder();
}
else
{
geocoder = new GoogleGeocoder(apiKey);
}
string longitude = string.Empty;
string latitude = string.Empty;
var xdoc = new XDocument();
var project = Project();
//Query for each project and get their longitude and latitude
DataTable dataTable = SqlHelper.ExecuteDataset("getAll", "1").Tables[0];
if (dataTable.Rows.Count > 0) {
foreach (DataRow dataRow in dataTable.Rows) {
Map.DataToObject(dataRow, project);
//Find Longitude and latitude based on zipcode or address.
IEnumerable<Address> addresses = geocoder.Geocode(project.SiteCity + "," + project.SiteState + " " + project.SitePostalCode);
longitude = addresses.First().Coordinates.Latitude.ToString();
latitude = addresses.First().Coordinates.Longitude.ToString();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://forecast.weather.gov/MapClick.php");
string uri = "http://forecast.weather.gov/MapClick.php?lat=" + latitude + "&lon=" + longitude + "&FcstType=dwml";
HttpResponseMessage response = await client.GetAsync(uri);
xdoc = XDocument.Parse(await response.Content.ReadAsStringAsync(), LoadOptions.None);
Services.Log.Info(xdoc.Descendants("wordedForecast").Descendants("text").ElementAt(0).Value);
//Update or create an weather entry for each project
}
}
return;//Task.FromResult(true);
}
}
您的网站似乎更改了政策。它需要设置User-Agent
header。您只需将其设置为某个值即可。
var url = "http://forecast.weather.gov/MapClick.php?lat=42&lon=-75&FcstType=dwml";
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Whosebug/1.0");
var xml = await client.GetStringAsync(url);