【问题标题】:passing custom object after serialization to WCF序列化后将自定义对象传递给 WCF
【发布时间】:2013-04-26 13:50:07
【问题描述】:

我有一个带有以下签名的 WCF 服务方法:

  public string CTNotification(JSONRecordingCompletedNotification content)
        {

我想创建一个客户端并使用它。我写了以下代码,但它给出了错误:

using (StreamReader sr = new StreamReader(Server.MapPath("~/json.txt")))
                {
                    string serviceBaseUrl = ConfigurationManager.AppSettings["serviceurl"].ToString() + "CTNotification";
                    string conversationId = ConfigurationManager.AppSettings["conversationId"].ToString();

                    String line = sr.ReadToEnd();
                    string jsonText = line;
                    string body = jsonText;

                JSONRecordingCompletedNotification RecordingCompletedNotification = new JavaScriptSerializer().Deserialize<JSONRecordingCompletedNotification>(body);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceBaseUrl);
                request.Method = "POST";
                request.ContentType = "application/json";                   



                using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
                {
                    writer.Write(RecordingCompletedNotification);
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Label1.Text = response.ToString();
            }   

我收到以下错误:

The remote server returned an error: (400) Bad Request.

【问题讨论】:

标签: asp.net wcf rest


【解决方案1】:

我建议您使用其中一个库进行 json 序列化和反序列化

  1. json.net

  2. fastjson

并将内容类型更改为

request.ContentType = "application/json; charset=utf-8";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多