使用 Json 错误请求的 WCF 服务

WCF service using Json Bad Request

我这辈子都弄不清楚发生了什么,我不能post使用json为我服务的人。我已经尝试阅读来自 google 的关于我遇到的问题的所有评论,但目前一切都让我陷入死胡同。请帮忙!

我通过 post 服务中的回调 URL 将 post 返回服务传递给第三方。第三方然后 post 使用回调 url 在 Json 中返回到我的 wcf 服务。我对最初的 post 没有问题,但他们和我自己都无法点击回调服务。我试过 Fiddler returns 一个 400 错误,但我不确定为什么。我需要的不仅仅是网络链接等来解决这个问题。请帮忙!

Web.config 文件

<system.serviceModel>
<services>
  <service behaviorConfiguration="serviceBehavior" name="IBVWebService.InstantBankVerificationPostBack">
    <endpoint address="http://localhost:64337/InstantBankVerificationPostBack.svc" behaviorConfiguration="web" binding="webHttpBinding" contract="IBVWebService.IInstantBankVerificationPostBack"></endpoint>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>
</behaviors>

网络界面

    [OperationContract]
    [WebInvoke(
    Method = "POST",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    RequestFormat = WebMessageFormat.Json)]
    void PostBack(String json);

测试客户端

        WebClient client = new WebClient();
        client.Headers["Content-type"] = "application/json";
        client.Encoding = System.Text.Encoding.UTF8;
        string jsonInput = "{'data':'testvalue'}";
        client.UploadString("http://localhost:64337/InstantBankVerificationPostBack.svc/PostBack", jsonInput);

当前跟踪日志。

我已经复制了你的场景,使用一个简单的 wcf 服务和你的配置以及一个简单的测试客户端:

WCF

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    void PostBack(String json);

客户:

string jsonInput = "{\"json\":\"testvalue\"}";
using (var client = new WebClient())
{
     client.Headers["Content-type"] = "application/json";
     client.Encoding = System.Text.Encoding.UTF8;
     client.UploadString("http://localhost:51175/Service1.svc/PostBack", "POST", jsonInput); 
}

在客户端中,确保您匹配 WCF 方法的签名,即您期望的对象被称为 json,因此当您调用该方法时从您的客户发送 json:'value'

另外,考虑对webclient使用using语句,确保用后处理。

您的方法参数名称是 "json",因此 JSON 输入参数应如下所示:

string jsonInput = "{'json':'testvalue'}";