2013-04-26 83 views
0

合格后自定义对象我有以下签名的WCF服务方法:序列化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. 
+0

您需要使用启用跟踪,看看有什么引擎盖下到底发生了改变的内容类型。 http://msdn.microsoft.com/en-us/library/ms733025.aspx – sebagomez 2013-04-26 13:57:31

+0

我做得正确吗? – DotnetSparrow 2013-04-26 14:02:33

+1

我这么认为......但我总是认为我正确地做到了:) – sebagomez 2013-04-26 14:14:11

回答

0

我会建议你使用任何的lib对JSON序列化和反序列化

  1. json.net

  2. fastjson

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