简体   繁体   English

WCF使用webHttpBinding-如何获取参数?

[英]WCF using webHttpBinding - how to take parameters?

I have a simple wcf method called ' test ' (the wcf service is configured for webHttpBinding ) 我有一个简单的wcf方法,称为“ test ”(wcf服务配置为webHttpBinding

I can make an HTTP POST to it fine without parameters, however I want to send it just a string as a single parameter but cant seem to achieve this? 我可以在没有参数的情况下使HTTP POST正常运行,但是我只想将字符串作为单个参数发送给它,但似乎无法实现?

For instance, the method test(string s){}; 例如,方法test(string s){};

I have tried posting to test with the data 'hello world' and '&s=hello world' to no avail. 我尝试过发布以test数据'hello world''&s=hello world'无济于事。

Edit: The interface for test looks something like: 编辑: test的界面看起来像:

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

test(string s)更改为test(Stream s)解决此问题。

You can try to post this as part of your request body: 您可以尝试将其发布为请求正文的一部分:

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

You can try it from fiddler. 您可以从提琴手那里尝试。 Or if you want to post from a .NET CLient use the below code@ 或者,如果您想从.NET CLient发布,请使用以下代码@

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);

You need to download the API called RestSharp for the above code to work 您需要下载名为RestSharp的API才能使上述代码正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM