0

我有一个带有以下签名的 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.
4

1 回答 1

0

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

  1. json.net

  2. 快速json

并将内容类型更改为

request.ContentType = "application/json; charset=utf-8";
于 2013-04-26T14:39:51.473 回答