gpt4 book ai didi

c# - 天气 API http ://www. Weather.gov/

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:06 25 4
gpt4 key购买 nike

我一直通过访问国家气象服务的 XML 文件来获取天气信息。但截至今天,我不断收到访问被拒绝错误 (403) 我的服务器被阻止了吗?如果是这样我可以使用什么来免费获取美国的天气信息?

我不敢相信我的网络服务仅仅几次点击就被阻止了。以防万一这是我用来测试天气数据的计划作业:

 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", "Stackoverflow/1.0");
var xml = await client.GetStringAsync(url);

关于c# - 天气 API http ://www. Weather.gov/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com