【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) { } }
上一篇: 很想叫他们一声“二”哥
推荐阅读
-
php使用$_POST或$_SESSION[]向js函数传参
-
【WCF Restful】Post传参示范
-
jQuery调用RESTful WCF示例代码(GET方法/POST方法)
-
解决vue处理axios post请求传参的问题
-
vue2.0 axios post请求传参问题(ajax请求)
-
vue2.0 axios post请求传参问题(ajax请求)
-
php使用$_POST或$_SESSION[]向js函数传参,_post_session
-
Jquery以及AngularJS 中 Get/Post传参的使用详情
-
在vue中通过axios处理post请求传参的问题(详细教程)
-
ajax通过post方式传参给后台controller,怎么获取传过来的参数