Calling REST Service from C#: Error 500

1.3k views Asked by At

I am trying to access a REST web service but get an error HTTP 500. There must be something I am not doing correct but couldn't find out. It is a publicly available service, otherwise I would have gotten a 403 error.

Here is my code: (url is a string containing the web service URL, param is a string containing the parameters.)

        string url = @"http://crimemapping.edmontonpolice.ca/DataProvider.asmx/getOccurrenceInfo";
        string param = "{\"method\":\"send\",\"params\":[\"neighbourhoodID\":\"2\",\"crimeTypes\":\"Assault\",\"strStartDate\":\"2011,12,02\",\"strEndDate\":\"2012,03,01\"]}";
        // Reassigning param to increase readability.
        param = @"{""method"":""send"",""params"":[""neighbourhoodID"":""2"",""crimeTypes"":""Assault;Sexual Assaults;Break and Enter;Theft From Vehicle;Homicide;Theft Of Vehicle;Robbery;Theft Over $5000"",""strStartDate"":""2012,02,01"",""strEndDate"":""2012,03,01""]}";

        string response;
        WebRequest request = WebRequest.Create(url);
        request.Method = "POST"; //REST based-services using Post method
        request.ContentType = "application/json"; //tells request the content typs is JSON
        request.ContentLength = param.Length;

        StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
        requestWriter.Write(param);
        requestWriter.Close();

        StreamReader responseReader = new StreamReader(request.GetResponse().GetResponseStream());
        response = responseReader.ReadToEnd();
        responseReader.Close();
2

There are 2 answers

0
Farhan On BEST ANSWER

What I couldn't figure out in the beginning was that the JSON request cannot be made by any IP. It checks the IP before proceeding. The error I was getting was way off, so didn't help much.

Thanks all.

7
b0rg On

what's the type of

param 

are you sure that this is json?

If you can see the request in fiddler just send the string from fiddler to verify that your .net infrastructure code is working.

But best thing to do is to grab the information from server, as it's been mentioned that 500 is internal server error. Error description will point out what is wrong.