1

我在 C# 中创建了一个 Web 服务,如下所示:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string UpdateHeatCallJSON(string json)
    {
        HeatItem item = JsonConvert.DeserializeObject<HeatItem>(json);
        UpdateHeatCall(item);
        HeatItemResponse response = new HeatItemResponse();
        //... more code
        return JsonConvert.SerializeObject(response);
    }

我基本上有一个对象 HeatItem,我想将其作为参数传入。

我目前在 C# 控制台应用程序中使用 Web 服务(用于测试)并且在寻找正确格式时遇到了挑战。这是我的电话:

   static void UpdateHeatItemJSON()
    {
        // corrected to WebRequest from HttpWebRequest
        WebRequest request = WebRequest.Create(requestServer + "/UpdateHeatCallJSON");

        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";
        string postData = "";
        postData = @"{'json':'{""BusinessPartner"":""00000000-0000-0000-0000-000000000000"",""CaseNumber"":4,""CaseDescription"":""first case"",""CaseType"":"""",""CaseSeverity"":"""",""DueDate"":""0001-01-01T00:00:00"",""AssignmentNumber"":5,""AssignmentDescription"":"""",""AssignmentCreation"":""0001-01-01T00:00:00"",""AssignmentTime"":""0001-01-01T00:00:00"",""ChangeDate"":""0001-01-01T00:00:00"",""ChangeTime"":""0001-01-01T00:00:00"",""Group"":"""",""SubGroup"":"""",""Module"":"""",""AssignmentStatus"":"""",""KPIChallenge"":false,""KPI1Status"":"""",""KPI1User"":"""",""KPI1Date"":""0001-01-01T00:00:00"",""KPI1Time"":""0001-01-01T00:00:00"",""KPI2Status"":"""",""KPI2User"":"""",""KPI2Date"":""0001-01-01T00:00:00"",""KPI2Time"":""0001-01-01T00:00:00"",""SessionID"":null}'}";

        System.Diagnostics.Debug.Print(postData);

        //get a reference to the request-stream, and write the postData to it
        using (Stream s = request.GetRequestStream())
        {
            using (StreamWriter sw = new StreamWriter(s))
                sw.Write(postData);
        }

        //get response-stream, and use a streamReader to read the content
        using (Stream s = request.GetResponse().GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(s))
            {
                string jsonData = sr.ReadToEnd();
                JObject jObject = JObject.Parse(jsonData);
                JToken jHeatItem = jObject["HeatItem"];
            }
        }
    }

看看上面的 postData,格式化 json 字符串是可行的,但是当我在http://jsonlint.com/验证字符串时,验证失败。当我像这样传入经过验证的 json 字符串时:

{“json”:{“BusinessPartner”:“00000000-0000-0000-0000-000000000000”,“CaseNumber”:4,“CaseDescription”:“第一个案例”,“CaseType”:“”,“CaseSeverity”:“” ,“DueDate”:“0001-01-01T00:00:00”,“AssignmentNumber”:5,“AssignmentDescription”:“”,“AssignmentCreation”:“0001-01-01T00:00:00”,“AssignmentTime”: “0001-01-01T00:00:00”、“更改日期”:“0001-01-01T00:00:00”、“更改时间”:“0001-01-01T00:00:00”、“组”:“” , "子组": "", "模块": "",“AssignmentStatus”:“”,“KPIChallenge”:假,“KPI1Status”:“”,“KPI1User”:“”,“KPI1Date”:“0001-01-01T00:00:00”,“KPI1Time”:“0001- 01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01-01T00: 00:00", "SessionID": null } }0001-01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01- 01T00:00:00", "SessionID": null } }0001-01-01T00:00:00”、“KPI2Status”:“”、“KPI2User”:“”、“KPI2Date”:“0001-01-01T00:00:00”、“KPI2Time”:“0001-01- 01T00:00:00", "SessionID": null } }

Web 服务返回 500 内部服务器错误,我认为这是因为它不喜欢传入的参数。我对此比较陌生,我想知道是否有人可以帮助我更好地理解这一点。

还查看响应(对于工作示例),返回 json 包含三个反斜杠 \\ 我相信可以转义引号。这使得这个电话

                JObject jObject = JObject.Parse(jsonData);
                JToken jHeatItem = jObject["HeatItem"];

失败。任何想法为什么?

谢谢托马斯

4

2 回答 2

0

在设置请求时,我添加了request.Accept = "application/json; charset=utf-8";. 确保使用该ScriptService属性装饰服务。例如,

[System.Web.Script.Services.ScriptService]    
public class Service1 : System.Web.Services.WebService

当您格式化您的帖子数据时,这种风格对我有用:

@"{""json"":'{""test"": ""value""}'}";

关键似乎是确保在初始冒号后的 JSON 中有一个单引号。

关于不需要的反斜杠字符,请查看此链接http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/。本质上,交易是您从服务中返回一个对象,而不是一个字符串。同样的事情也适用于将对象传递到您的服务中。

于 2012-10-08T03:52:28.407 回答
0

感觉自己像个白痴,在这上面浪费了很多时间,现在一切都那么容易。让 asp.net 处理它。按照这篇文章中的步骤进行操作,一切正常

http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/

于 2012-10-08T11:32:15.327 回答