来自c#的JSON帖子失败,出现500错误

时间:2015-02-07 13:27:34

标签: c# jquery asp.net web-services

我有一个c#winforms应用程序,我试图调用一个我控制代码的Web服务(两端)。我知道Web服务很好,因为我可以从jquery中调用它而没有任何问题,并且返回了有效的json。但是当我从c#调用它时,我收到此错误:"远程服务器返回错误:(500)内部服务器错误。"在调试器中,我无法收集到更多细节。

这是我的c#来电代码:

private string GetMemberCheckInData() {
    string response = string.Empty;
    string fullURL = RootServiceURL + "MemberCheckIn";
    var httpWebRequest = (HttpWebRequest) WebRequest.Create(fullURL);
    httpWebRequest.ContentType = "text/json";
    httpWebRequest.Method = "POST";

    using(var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
        string json = "{\"scanCode\":\"" + APIKey + "\"," +
            "\"siteID\":\"2\"," +
            "\"revenueGroupID\":\"0\"," +
            "\"employeeID\":\"12345\"}";;

        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();

        var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();

        using(var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
            response = streamReader.ReadToEnd();
        }
    }

    return response;
}

以下是正在发送的文字JSON:

{"scanCode":"***8G9V3MA39H0N****","siteID":"2","revenueGroupID":"0","employeeID":"12345"}

这是c#web服务代码(从jquery调用时工作正常):

[WebMethod]
public void MemberCheckIn(string scanCode, int siteID, int revenueGroupID, int employeeID)
{
   ...code is never reached...
}

实际上从未输入过Web服务功能;我用调试器证实了这一点。当我从jquery调用它时,我可以调试它。因此,似乎该请求被.net运行时或IIS等拒绝。

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的网络方法希望您将参数传递给它。

string fullURL = RootServiceUrl + "MemberCheckIn?scanCode=X&siteID=1...etc.