如何向WCF POST方法(RESTful服务)传递参数?

3
我正在处理基于 WCF 的 Rest 服务。我在服务中编写了 Get 和 Post 方法,并且当我在 URL 中键入(以 JSON 格式)时,Get 方法能够正常工作(获取数据)。
问题在于,当我尝试对 POST 方法执行相同操作时,URL 跳转到另一个页面 "页面未找到..."。
我了解到 POST 方法需要提交表单来处理请求。
因此,我尝试使用 Chrome 扩展程序(Simple Rest 客户端、Advanced Rest 客户端、Post man Rest 客户端)和 Fiddler。
下面是我发布的服务方法 - Get 方法(接口方法声明)。
[OperationContract]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, 
ResponseFormat =   WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, 
UriTemplate = "GetCategoryTypes/")]
List<CategoryType> GetCategoryTypes();

这是我的POST方法。
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddOrders/", 
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest)]
int AddOrders(decimal amount, int tableID, DateTime orderDate, int isActive);

这是我用于服务的web.config文件。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
   </system.web>  
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ServiceBehaviour" allowCookies="true" messageEncoding="Mtom" />
      </basicHttpBinding>
      <webHttpBinding>
        <binding name="ServiceBehaviour1" allowCookies="true"/>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="EMC.DD.ServiceLayer.Service1" ehaviorConfiguration="ServiceBehaviour">
        <endpoint address="http://localhost/EMCService/Service1.svc" 
           binding="basicHttpBinding" contract="EMC.DD.ServiceLayer.IService1">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>      
      </service>
      <service name="EMC.DD.ServiceLayer.Service2" 
         behaviorConfiguration="ServiceBehaviour1">
        <endpoint address="http://localhost/EMCService/Service2.svc" 
          binding="webHttpBinding" behaviorConfiguration ="web" 
          contract="EMC.DD.ServiceLayer.IService2">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>     
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name ="ServiceBehaviour">     
          <serviceMetadata httpGetEnabled="true" />      
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
        <behavior name ="ServiceBehaviour1">       
          <serviceMetadata httpGetEnabled="true" />        
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name ="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors> 
  </system.serviceModel>
 <system.webServer>
        <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

我不确定我的构造方法(POST 方法)是否有任何错误,或者测试它的方式是否正确。

我需要你们所有专家的帮助,我已经为这个问题奋斗了2天,最后我来这里发帖求助。

非常感谢任何帮助。


你只是想测试你的方法吗?当你使用Fiddler进行上述POST操作时,它没有触发吗? - crthompson
我确实想知道我构建的方法是否正确,以及如何测试后置方法(以检查方法是否正确实现)。@paqogomez - G K
你能在 Fiddler 中看到请求吗?你能看到服务器返回的状态码吗? - Yuval Itzchakov
1个回答

9

经过多次尝试和调整,我终于找到了解决POST请求问题的方法。在这里,我将发布我的实现方案。

  [OperationContract]
  [WebInvoke(Method = "POST", UriTemplate = "AddOrders", RequestFormat =   
  WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,BodyStyle = 
  MessageBodyStyle.Bare)]
  int AddOrders(RequestData orderRequestData);

这是用于实现操作合同的内容。
客户端应用程序:
        WebClient WC = new WebClient();
        WC.Headers.Add("Content-Type", "application/json");
        WC.Encoding = Encoding.UTF8;

        MemoryStream MS = new MemoryStream();
        DataContractJsonSerializer JSrz = new 
        DataContractJsonSerializer(typeof(RequestData));
        JSrz.WriteObject(MS, order);
        string data = Encoding.UTF8.GetString(MS.ToArray(), 0, (int)MS.Length);

        byte[] res1 = 
        WC.UploadData("http://localhost/EMCService/Service2.svc/AddOrders", "POST",MS.ToArray());

        MS = new MemoryStream(res1);
        JSrz = new DataContractJsonSerializer(typeof(int));
        int result = (int)JSrz.ReadObject(MS);

我没有进行任何配置设置,仍在使用我在上面的问题中发布的web.config的旧设置,并且它正在工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接