VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • 【WCF Restful】Post传参示范

1、传多个参数

接口定义:(ResponseFormat与RequestFormat分别将相应参数序列化、请求参数反序列化)

[OperationContract]
[WebInvoke(UriTemplate = "api/fun2", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
 string TestFun2(string p1,string p2);

实现:

public string TestFun2(string p1, string p2)
{
   return p1+p2; 
}

调用:

复制代码
private void button1_Click(object sender, EventArgs e)
{
   try
   {
      string sUrl3 = "http://localhost:10086/api/fun2";
      string sBody2 = JsonConvert.SerializeObject(new { p1 = "1", p2 = "2" });

      HttpHelper.HttpPost(sUrl3, sBody2);
   }
   catch (Exception ex)
   {}
}
复制代码

HttpHelper.cs

HttpHelper

 

2、传对象

 接口定义:

[OperationContract]
[WebInvoke(UriTemplate = "api/fun", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)]
 string TestFun(TestModel data);

实现:

复制代码
 public string TestFun(TestModel pars)
 {
    try
    {
       return pars.Test1 + pars.Test2;
    }
    catch (Exception ex)
    {}
}
复制代码

调用:

复制代码
private void button1_Click(object sender, EventArgs e)
{
   try
   {
      string sUrl = "http://localhost:10086/api/fun";
                
      TestModel model = new TestModel();
      model.Test1 = "1";
      model.Test2 = "2";

      string sBody = JsonConvert.SerializeObject(model);
               
      HttpHelper.HttpPost(sUrl, sBody);
   }
   catch (Exception ex)
   { }
}
 


相关教程