简体   繁体   English

从REST客户端向WCF服务发布请求

[英]Post Request to WCF service from a REST client

In my current project, I am sending a POST request to a WCF service. 在当前项目中,我正在向WCF服务发送POST请求。 But, I am getting empty response. 但是,我得到的回应是空洞的。 I have tried to use the similar post on stackoverflow : POST1 and [POST2][2] , but I could not solve the problem. 我试图在stackoverflow上使用类似的帖子: POST1和[POST2] [2],但是我无法解决问题。 My WCF service code is as follows: 我的WCF服务代码如下:

namespace RestfulWCFService 
{

  [OperationContract]
    [WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Xml, UriTemplate="xml/?firstname={firstname}&lastname={lastname}")]
    string SayHelloXml(string firstname, string lastname);

    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{name}")]
    string SayHelloJson(string name);



    [ServiceContract]
    public interface IRestfulTestService 
    {

        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "/data")]
        string SayHelloJSONPOSTRequest(string jsonRequestString);

    }   
} 

The implementation of the interface is as follows: 接口的实现如下:

namespace RestfulWCFService
{

       string IRestfulTestService.SayHelloXml(string firstname, string lastname)
    {
        return "Hello  " + firstname + " " +  lastname;
    }

    string IRestfulTestService.SayHelloJson(string name)
    {
        return "Hello  " + name;
    }


    public class RestfulTestService : IRestfulTestService
    {
      string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonRequestString)
        {
            return "Hello  " + jsonRequestString; 
        }

    }
}

Now, from a REST client, my request is as follow: 现在,从REST客户端,我的要求如下:

 http://localhost/RestfulWCFService/RestfulTestService.svc/data 

and the Content-Type:application/json and payload is {"firstname":"Pankesh"} . 并且Content-Type:application/json和有效负载为{"firstname":"Pankesh"} I am getting no response from WCF. 我没有收到WCF的任何回复。

For your reference, I am attaching the screenshot of my client. 供您参考,我附上我的客户端的屏幕截图。

在此处输入图片说明

In the interface you use: 在界面中使用:

string SayHelloJSONPOSTRequest(string jsonRequestString);

But in the implementation of the interface you use: 但是在实现接口时您使用:

string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonString)

One mistake could be the different naming of the json-String-parameter. 一个错误可能是json-String-parameter的不同命名。

EDITS: 编辑:

Firstly try to set content-length in the raw headers in the tool you use above. 首先,尝试在上面使用的工具的原始标头中设置content-length。

Another possible mistake is tha the variable you use in the return statement is not the same as the parameter you given to the method. 另一个可能的错误是,您在return语句中使用的变量与您赋予该方法的参数不同。

string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonRequestString)
{
     return "Hello  " + jsonString; 
}

It's probably because you set BodyStyle to WebMessageBodyStyle.Wrapped . 可能是因为您将BodyStyle设置为WebMessageBodyStyle.Wrapped

When you use Wrapped you should post your data as: 使用Wrapped您应将数据发布为:

{"jsonRequestString":{"firstname":"Pankesh"}}

Alternatively you can change it to WebMessageBodyStyle.Bare , and send it as: 或者,您可以将其更改为WebMessageBodyStyle.Bare ,并将其发送为:

{"firstname":"Pankesh"}

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

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