theodi / uk-postcodes

MIT License
41 stars 17 forks source link

GET POSTCODE DATA WITHIN X DISTANCE IN MILES RETURNING ERROR 500 #47

Open calmotuns opened 8 years ago

calmotuns commented 8 years ago

I have the below code: it returns internal server error, but whenever I post "http://uk-postcodes.com/postcode/nearest?postcode=SE146NS&miles=1&format=json" directly to the browser before running the code it runs fine. If I run the code without posting directly to browser it returns internal server error. Please is there anything I'm doing wrong

Code: string baseUrl = "http://uk-postcodes.com/postcode/nearest"; string restUrl = string.Format("{0}?postcode={1}&miles={2}&format=json", baseUrl, postcode, miles);

            HttpWebResponse response = null;
            HttpWebRequest request = WebRequest.Create(restUrl) as HttpWebRequest;
            request.Method = "GET";
            request.Accept = "application/json";
            request.ContentType = "application/json";

            using (response = request.GetResponse() as HttpWebResponse)
            {
                var reader = new StreamReader(response.GetResponseStream());

                if (response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Found)
                {
                    string responseContent = reader.ReadToEnd();

                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    postCodeWithRadiuses = javaScriptSerializer.Deserialize<List<PostCodeWithRadius>>(responseContent);

                }
                reader.Close();
            }
calmotuns commented 8 years ago

the postcode variable is a real postcode amd the miles=1