我正在尝试调用 WCF 剩余服务,如下所示:
[WebInvoke(UriTemplate = "Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string Process(string AuthenticationInfo)
{
我正在尝试在 Fiddler 2 中使用以下内容调用它:
User-Agent: Fiddler
Host: localhost
content-type: application/json;charset=utf-8
content-length: 0
data: {"AuthenticationInfo": "data"}
我在方法中有一个断点,它确实遇到了断点,但 AuthenticationInfo 的值始终为空,而不是“数据”。
我做错了什么?
谢谢。
最佳答案
[WebInvoke]
属性的默认“body style”是“Bare”,这意味着输入(在你的例子中,“data”
)必须是“按原样”发送。您发送的是输入的包装版本(即,包装在一个对象中,该对象的键是参数名称。
有两种方法可以实现此目的:要么更改 WebInvoke
声明以包含 BodyStyle 参数:
[WebInvoke(
UriTemplate = "Login",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string Process(string AuthenticationInfo)
或者你可以改变发送参数“bare”的请求:
POST .../Login HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Type: application/json;charset=utf-8
Content-Length: 6
"data"
关于wcf - 无法使用 Fiddler 将 JSON 发布数据传递到 WCF REST 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10290210/