WCF使用webHttpBinding -如何接受参数

本文关键字:何接受 参数 使用 webHttpBinding WCF | 更新日期: 2023-09-27 18:17:54

我有一个简单的wcf方法叫做' test ' (wcf服务是为webHttpBinding配置的)

我可以使一个HTTP POST到它没有参数,但我想发送它只是一个字符串作为一个参数,但似乎不能实现这一点?

例如,方法test(string s){};

我已经尝试将'hello world''&s=hello world'的数据发布到test,但无济于事。

编辑:test的界面看起来像:

[OperationContract]
[WebInvoke(UriTemplate = "test", Method = "POST", RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
string test(string s);

WCF使用webHttpBinding -如何接受参数

test(string s)更改为test(Stream s)修复了此问题。

您可以尝试将此作为请求正文的一部分:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello World</string>

你可以试试fiddler。或者,如果你想从。net客户端发布,使用下面的代码@

var client = new RestClient();  
client.BaseUrl = serviceBaseUrl;  
var request = new RestRequest(method){RequestFormat = DataFormat.Xml};      
request.Resource = resourceUrl;  
request.AddParameter("text/xml", requestBody,  ParameterType.RequestBody);  
var response = client.Execute(request);

您需要下载名为RestSharp的API才能使上述代码工作