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

最新文章

  1. Java概述
  2. SQL Server中使用PIVOT行转列
  3. Matter.js – 你不能错过的 2D 物理引擎
  4. Python3 学习笔记------迭代器
  5. 【转】 js怎么区分出点击的是鼠标左键还是右键?
  6. paramiko模块,线程,进程
  7. 项目解析- JspLibrary - part3
  8. Java API —— File类
  9. Unity3d Realtime Dynamic Volume Clouds Rendering
  10. java13 InputStream,Reader
  11. Custom Media Player in WPF (Part 1)
  12. ZOJ2006 一道很尴尬的string操作题
  13. 苹果新的编程语言 Swift 语言进阶(一)--综述
  14. java基础(四):谈谈java中的IO流
  15. linux xfs的一次io异常导致的crash
  16. Window配置环境变量
  17. ISO 8895-1
  18. Java 集合框架(二)—— ArrayList
  19. Java Graphics 图形绘制
  20. Let&#39;s call it a &quot;return&quot;

热门文章

  1. IDEA更换banner(娱乐专用)
  2. python基础面试集锦(51-100)
  3. AS中加载gradle时出现Gradle sync failed: Could not find com.android.tools.build:gradle.的错误
  4. 暑假第二周总结(在centos系统中安装eclipse出错,改为安装ubantu)
  5. 今天更新IDEA后,我依旧要永久激活(支持2019.3.3版本)
  6. Go语言标准库之net/http
  7. Codeforces_496_E_贪心
  8. Android Studio MainActivity中的R为红色
  9. ARTS Week 3
  10. jquery deferred 转载