Sca*_*lin 1 .net c# mobile azure
我通过访问他们的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);
}
}
Run Code Online (Sandbox Code Playgroud)
看来你的网站改变了政策.它需要User-Agent设置标题.您所需要的只是将其设置为某个值.
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1609 次 |
| 最近记录: |