欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【WCF Restful】Post传参示范

程序员文章站 2023-09-28 16:37:56
1、传多个参数 接口定义: [OperationContract] [WebInvoke(UriTemplate = "api/fun2", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebM ......

1、传多个参数

接口定义:

[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

/// <summary>
        /// httppost (application/json)
        /// </summary>
        /// <param name="url"></param>
        /// <param name="body"></param>
        /// <param name="contenttype"></param>
        /// <returns></returns>
        public static string httppost(string url, string body)
        {
            string responsecontent = string.empty;
            httpwebrequest httpwebrequest = (httpwebrequest)webrequest.create(url);
            httpwebrequest.contenttype = "application/json";
            httpwebrequest.method = "post";
            httpwebrequest.timeout = 200000000;
            //httpwebrequest.keepalive = true;
            httpwebrequest.maximumresponseheaderslength = 40000;

            byte[] btbodys = encoding.utf8.getbytes(body);
            httpwebrequest.contentlength = btbodys.length;

            using (stream writestream = httpwebrequest.getrequeststream())
            {
                writestream.write(btbodys, 0, btbodys.length);
            }

            using (httpwebresponse httpwebresponse = (httpwebresponse)httpwebrequest.getresponse())
            {
                using (streamreader streamreader = new streamreader(httpwebresponse.getresponsestream()))
                {
                    responsecontent = streamreader.readtoend();
                }
            }

            return responsecontent;
        }

 

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)
   { }
}