标签:
用来与HTTP 模式的WCF 无缝隙切换。
服务端是MVC。
在Home/About 上面加 [HttpPost]。读取Request.InputStream 中所有内容。然后返回JSON给客户端:return Json(c1);。
[HttpPost] public ActionResult About() { string dfCharSet = "utf-8"; if (Request.QueryString["charset"] != null) dfCharSet = Request.QueryString["charset"]; Encoding ec = Encoding.GetEncoding(dfCharSet); string inStr = string.Empty; using (StreamReader sr = new StreamReader(Request.InputStream, ec)) { inStr = sr.ReadToEnd(); } Models.Class1 c1 = new Models.Class1(); c1.name = "服务器"; c1.inStr = inStr;
Response.ContentEncoding = ec; return Json(c1); }
客户端。POST JSON 到服务端,接收服务端返回的JSON。建议用HttpWebRueqest.
try { Class1 c1 = new Class1(); c1.name = "客户端"; c1.inStr = ""; string input = JsonConvert.SerializeObject(c1); byte[] bts = Encoding.UTF8.GetBytes(input); byte[] btRst; using (WebClient wc = new WebClient()) { string url = "http://localhost:52844/Home/About"; wc.Encoding = Encoding.UTF8; btRst = wc.UploadData(url, "post", bts); } string rst = Encoding.UTF8.GetString(btRst); MessageBox.Show(rst); } catch (Exception ex) { MessageBox.Show(ex.Message); }
-
C# .NET MVC 接收 JSON ,POST,WCF 无缝隙切换
标签:
原文地址:http://www.cnblogs.com/runliuv/p/5927781.html