wcf在post请求时,关于string类型参数传入中文的处理

一、方法默认只有一个参数

(1)BodyStyle = WebMessageBodyStyle.Bare

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[Description("地址解析(多个地址以','间隔,解析结果以JSON格式返回)")]
string Parse(string address);

------------------------------------------------------------------------------------

c#代码在客户端调用时,使用以下语句:

//SufeiHttpHelper是一个第三方的http请求调用工具

SufeiHttpHelper helper = new SufeiHttpHelper();
var item = new HttpItem();
item.URL = "http://localhost:35401/parse";
item.ContentType = "application/json";
item.Method = "post";
item.PostEncoding = Encoding.GetEncoding("utf-8");
item.Postdata = "\"天津大学\"";

(2)BodyStyle = WebMessageBodyStyle.Wrapped

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[Description("地址解析(多个地址以','间隔,解析结果以JSON格式返回)")]
string Parse(string address);

---------------------------------------------------------------------------------------

item.PostEncoding = Encoding.GetEncoding("utf-8");

item.Postdata = "{\"address\":\"天津\"}";             //包装成json格式,并且指定参数名称

总结:以上两个形式,在传入string参数为中文时,必须要包装成json,如果是数字或字母,在BodyStyle = WebMessageBodyStyle.Bare),可以直接传入,无需包装成json

 

一、方法有多个参数

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)]
Stream HelloDataStr(String data1,string data2);

由于是多个参数,BodyStyle必须为 WebMessageBodyStyle.Wrapped,否则参数无法映射。

客户端调用:

item.PostEncoding = Encoding.GetEncoding("utf-8");

item.Postdata = {\"data1\":\"hh\",\"data2\":\"ss\"}");

转载于:https://www.cnblogs.com/SimpleGIS/p/9938036.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值