【WCF Restful】Post传参示范
2024-10-08 08:15:25
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)
{ }
}
最新文章
- Java概述
- SQL Server中使用PIVOT行转列
- Matter.js – 你不能错过的 2D 物理引擎
- Python3 学习笔记------迭代器
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
- paramiko模块,线程,进程
- 项目解析- JspLibrary - part3
- Java API —— File类
- Unity3d Realtime Dynamic Volume Clouds Rendering
- java13 InputStream,Reader
- Custom Media Player in WPF (Part 1)
- ZOJ2006 一道很尴尬的string操作题
- 苹果新的编程语言 Swift 语言进阶(一)--综述
- java基础(四):谈谈java中的IO流
- linux xfs的一次io异常导致的crash
- Window配置环境变量
- ISO 8895-1
- Java 集合框架(二)—— ArrayList
- Java Graphics 图形绘制
- Let&#39;s call it a ";return";
热门文章
- IDEA更换banner(娱乐专用)
- python基础面试集锦(51-100)
- AS中加载gradle时出现Gradle sync failed: Could not find com.android.tools.build:gradle.的错误
- 暑假第二周总结(在centos系统中安装eclipse出错,改为安装ubantu)
- 今天更新IDEA后,我依旧要永久激活(支持2019.3.3版本)
- Go语言标准库之net/http
- Codeforces_496_E_贪心
- Android Studio MainActivity中的R为红色
- ARTS Week 3
- jquery deferred 转载