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. 通过指针突破C++类的访问权限

    看如下代码 #include "pch.h" #include <iostream> using namespace std; class A { public: A( ...

  2. 倒转数组 Leetcode189

    倒转数组 Leetcode189 记录调整数组顺序而不需要另加内存的一种方法: 题目 189. 旋转数组 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [ ...

  3. jsp简单实现交互

    test.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&quo ...

  4. 分析Ajax爬取今日头条街拍美图-崔庆才思路

    站点分析 源码及遇到的问题 代码结构 方法定义 需要的常量 关于在代码中遇到的问题 01. 数据库连接 02.今日头条的反爬虫机制 03. json解码遇到的问题 04. 关于response.tex ...

  5. vagrant相关

    无法挂载共享目录,报错如下 Vagrant was unable to mount VirtualBox shared folders. This is usually because the fil ...

  6. Codeforces_729_E

    http://codeforces.com/problemset/problem/729/E 存在的数必须连续,若不连续,从最后选人来补,注意根不是0和其他点是0的情况. #include<io ...

  7. c++ 初始化列表和构造函数初始化区别

    先上代码 #include <iostream> class MyContruct { public: MyContruct() { std::cout << "My ...

  8. 在.NET Core中使用MachineKey

    在.NET Core中使用MachineKey 姐妹篇:<ASP.NET Cookie是怎么生成的> 姐妹篇:<.NET Core验证ASP.NET密码> 在上篇文章中,我介绍 ...

  9. php 绘制验证码 示例

    <?php header("content-type:image/jpeg"); session_start();//开启session //宽高 字体大小 $width=1 ...

  10. linux 统计文件夹下文件,文件夹,所有个数

    统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...