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 = "", p2 = "" }); 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 = ;
//httpWebRequest.KeepAlive = true;
httpWebRequest.MaximumResponseHeadersLength = ; byte[] btBodys = Encoding.UTF8.GetBytes(body);
httpWebRequest.ContentLength = btBodys.Length; using (Stream writeStream = httpWebRequest.GetRequestStream())
{
writeStream.Write(btBodys, , btBodys.Length);
} using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
{
responseContent = streamReader.ReadToEnd();
}
} return responseContent;
}

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 = "";
model.Test2 = ""; string sBody = JsonConvert.SerializeObject(model); HttpHelper.HttpPost(sUrl, sBody);
}
catch (Exception ex)
{ }
}

【WCF Restful】Post传参示范的更多相关文章

  1. 带Boolean类型的参数的接口用postman测试时传参问题

    带Boolean类型的参数的接口用postman测试时传参问题 @Data public class ATest { private Boolean isCommit; } postman 测试时传参 ...

  2. 利用WCF与Android实现图片上传并传参

    利用WCF与Android实现图片上传并传参 最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想 ...

  3. RESTful Get方式传参json格式后端400 解决方案

    前端采用vue+axios 后端采用spring boot restful 问题: 前端get 请求需要传递array 字段值 后端由于tomcat 版本问题,不支持url接受特殊字符包括 [] {} ...

  4. SpringBoot:使用feign调用restful服务时地址栏传参

    1.服务提供者(controller层) @GetMapping("/user/{id}") public ApiResult getById(@PathVariable(&quo ...

  5. 使用多种客户端消费WCF RestFul服务(四)——Jquery篇

    Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...

  6. C# WebApi传参之Post请求-AJAX

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    上一节讲述了C# WebApi传参之Get请求 ...

  7. Web API中的传参方式

    在Restful风格的WebApi的里面,API服务的增删改查,分别对应着Http Method的Get / Post / Delete /Put,下面简单总结了Get / Post /Delete ...

  8. MyBatis 强大之处 多环境 多数据源 ResultMap 的设计思想是 缓存算法 跨数据库 spring boot rest api mybaits limit 传参

    总结: 1.mybaits配置工2方面: i行为配置,如数据源的实现是否利用池pool的概念(POOLED – This implementation of DataSource pools JDBC ...

  9. 二十一、springboot之定制URL匹配规则(项目中遇到的问题:get方式传参,带有小数点,被忽略)

    一.问题描述: get方式传参,在传送价格,积分时(带有小数点),debug后台微服务接受到的参数,却不带小数点,如:price是0.55,后台接受后却是0 二.解决 在WebConfiguratio ...

随机推荐

  1. SpringBoot学习(三):日志

    1.日志框架 小张:开发一个大型系统: ​ 1.System.out.println(""):将关键数据打印在控制台:去掉?写在一个文件? ​ 2.框架来记录系统的一些运行时信息: ...

  2. jQuery下载所有版本

    下载地址:http://www.jq22.com/jquery-info122

  3. C++ 函数详解

    C++函数完全兼容C函数定义的风格,但是也做出了一些改进.一方面,C++函数允许使用缺省参数值和占位参数:另一方面,C++提供了重要的函数重载机制:另外,为了解决C中臭名昭著的宏缺陷问题,C++提供了 ...

  4. Codeforces_731_A

    http://codeforces.com/problemset/problem/731/A 每次操作总共4种情况,判断一下就好了. #include<iostream> #include ...

  5. HDU6440 Dream(费马小定理+构造) -2018CCPC网络赛1003

    题意: 给定素数p,定义p内封闭的加法和乘法,使得$(m+n)^p=m^p+n^p$ 思路: 由费马小定理,p是素数,$a^{p-1}\equiv 1(mod\;p)$ 所以$(m+n)^{p}\eq ...

  6. POJ 1789 Truck History (Kruskal最小生成树) 模板题

    Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...

  7. Go语言实现:【剑指offer】替换空格

    该题目来源于牛客网<剑指offer>专题. 请实现一个函数,将一个字符串中的每个空格替换成"%20". 例如,当字符串为We Are Happy.则经过替换之后的字符串 ...

  8. DOCKER绝对领域从2048到4069?不:25519,数字的飞跃,HTTP/2

    这个标题花了几分钟,远远超过我构思以下内容的时间损耗,希望大家且看且珍惜,因为这是为数不多的cnblog特别标题 我记得很久以前,我开了一系列随笔,从第一篇揭发233的docker/machine开始 ...

  9. Xcode11: 删除默认Main.storyBoard, 自定义UIWindow的变化 UIWindow 不能在AppDelegate中处理

    Xcode自动新增了一个SceneDelegate文件,查找了一下官方文档WWDC2019:Optimizing App Launch 发现,iOS13中appdelegate的职责发现了改变: iO ...

  10. 打包 压缩 命令tar zip

    tar语法 #压缩tar -czvf ***.tar.gztar -cjvf ***.tar.bz2#解压缩tar -xzvf ***.tar.gztar -xjvf ***.tar.bz2 tar ...