RESTful WCF Web 服务 POST 问题
我无法将参数传递给 wcf Web 服务。我的网络方法:
<前><代码> [操作合同] [WebInvoke(方法 = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate =“playersJson2”)] 列表<人员> GetPlayers(字符串名称1,字符串名称2);
当我发出 http post 请求时,我得到了 200 OK 响应,格式正确,但 Web 服务似乎无法获取参数(name1,name2)。 Wireshark 显示如下:
您发现有什么问题吗?
更新:不确定这是否重要,但我的服务正在使用“webHttpBinding”并且发布请求来自 Android。
I can't pass over parameters to wcf web service. My web method:
[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "playersJson2")] List<Person> GetPlayers(string name1, string name2);
When I make http post request, I got 200 OK response with correct json form but web service seems to fail to get paramters (name1, name2). Wireshark shows following:
Do you see anything wrong?
Update: Not sure it matters but my service is using "webHttpBinding" and post request is coming from Android.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WCF 不支持开箱即用的表单/编码数据。其他答案提到了一些替代方案(以 Stream 形式接收输入,将请求更改为 JSON)。另一种替代方法是使用理解表单 urlencoded 请求的自定义格式化程序,它不会强制您更改请求或操作。下面的代码显示了执行此操作的代码。
WCF doesn't support forms/encoded data out-of-the box. The other answers mentioned some alternatives (receive the input as a Stream, change the request to JSON). Another alternative, which doesn't force you to change the request or the operation is to use a custom formatter which understands form-urlencoded requests. The code below shows one which does that.
看来您需要解析您的帖子数据手册...
您可以查看 此处 例如
It looks like you need to parse your post data manual...
You can look here for example
唯一看起来不合适的是
playerJson2
但这只是因为我以前从未使用过 UriTemplate。您是否可以在没有 UriTemplate 的情况下使其工作并仅发布到/WcfService1/Service1.svc/GetPlayers
?您在该项目中还有其他 WCF 服务吗?The only thing that looks out of place is the
playerJson2
but that is just because I have never used the UriTemplate before. Can you get it to work without the UriTemplate and just post to/WcfService1/Service1.svc/GetPlayers
? Are there any other WCF services that you have working in the project?您需要设置正确的Content-Type
,在本例中为application/json
。抱歉误读了您的问题。按如下方式更新您的 UriTemplate:
You need to set the correctContent-Type
which isapplication/json
in this case.Sorry for misreading your question. Update your UriTemplate as follows: