1

I have been getting weather information from the National Weather service by accessing their XML files. But as of today I keep getting an access denied error (403) Has my server been block? if so What can I use to get weather info for free in the United States ?

I can't believe my web service was block with just a couple of hits. Just in case this is the schedule job I was using to test the weather data:

 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);
    }
}
ScarletMerlin
  • 465
  • 4
  • 24
  • 4
    I'm voting to close this question as off-topic because it is questioning the server status of weather.com rather than any actual programming question. – crthompson Apr 02 '15 at 20:26
  • I wasn't sure if it was my code of the server. – ScarletMerlin Apr 02 '15 at 20:26
  • If your code was working previously, and isnt working now, how would it be your code? Either way, you should be asking weather.com, not stackoverflow. – crthompson Apr 02 '15 at 20:27
  • I had an old application that did something similar, but I changed a good bit of the code. I will close the question anyways. I got what I wanted. – ScarletMerlin Apr 02 '15 at 20:29

1 Answers1

6

It seems your site changed policy. It requires User-Agent header be set. All you need is to set it to some value.

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);
EZI
  • 15,209
  • 2
  • 27
  • 33
  • 2
    You sir are my hero. I tried everything I could, but you saved me. and to believe I almost closed the question. It works now, Thanks a lot. – ScarletMerlin Apr 02 '15 at 20:45