在 POST 操作期间将字符串参数传递给 RESTful 服务

发布于 2024-12-12 09:28:00 字数 800 浏览 5 评论 0原文

我有一个使用以下方法的 RESTful 服务:

[WebInvoke] 
string GetDataFromStringAsString(string xmlString); 

我的客户端对该方法的调用如下:

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

让我们将一个字符串作为“Hello World”发布。

现在我发布到上述方法的字符串给了我 400 Bad 要求。为了让它工作,我必须将上面的字符串包裹起来 如下所示的元素:

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

现在,当我发布上面的字符串时,我会收到来自的成功响应 服务器。

为什么我必须手动包裹字符串才能使其工作。是 有一种方法可以让我无需执行以下操作即可发布字符串 以上手动。

I am having a RESTful service with the following method:

[WebInvoke] 
string GetDataFromStringAsString(string xmlString); 

My client call to the method is as below:

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

Let us take a string to post as "Hello World".

Now the string that i post to the above method gives me a 400 Bad
request. In order to get it working i had to wrap the above string in
a element as shown below:

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

Now when i post the above string i get a success response back from
the server.

Why is that i have to manually wrap the string to make it work. Is
there a way that i can achieve to post a string without doing the
above manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人骨 2024-12-19 09:28:00

我知道的唯一其他方法是使用流作为输入参数。例如,

[WebInvoke] 
string GetDataFromStringAsString(stream xmlString);

.Net 4 WCF REST 的问题在于,从根本上来说,WCF 只知道如何传递两种类型的信息,即 XML 或字节流。就我个人而言,我会使用 WCF Web API 而不是标准 WCF REST 库,因为您将遇到更多此类问题。

The only other way that I am aware of is to use stream as your input parameter. e.g.

[WebInvoke] 
string GetDataFromStringAsString(stream xmlString);

The problem with .Net 4 WCF REST is that fundamentally WCF only knows how to pass two types of info, either XML or a stream of bytes. Personally, I would use WCF Web API instead of the standard WCF REST library because you are going run into lots more of these kinds of issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文