用来与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); }

-

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-11
  • 2021-12-13
  • 2022-01-08
  • 2021-11-30
  • 2021-11-27
猜你喜欢
  • 2021-08-13
  • 2021-07-14
  • 2021-06-17
  • 2021-12-29
  • 2022-02-09
  • 2022-03-06
  • 2021-11-21
相关资源
相似解决方案