从C#调用REST服务:错误500

时间:2012-03-02 16:42:16

标签: c# .net web-services rest

我正在尝试访问REST Web服务但是收到错误HTTP 500.必须有一些我做得不正确但无法找到的东西。这是一个公开的服务,否则我会得到403错误。

这是我的代码: (url是包含Web服务URL的字符串,param是包含参数的字符串。)

        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 个答案:

答案 0 :(得分:0)

的类型是什么
param 
你知道这是json吗?

如果您可以在fiddler中看到请求,只需从fiddler发送字符串以验证您的.net基础结构代码是否正常工作。

但最好的办法是从服务器获取信息,因为有人提到500是内部服务器错误。错误描述将​​指出错误。

答案 1 :(得分:0)

我在开始时无法弄清楚的是,任何IP都无法生成JSON请求。它会在继续之前检查IP。我得到的错误很少,所以没有多大帮助。

谢谢大家。