用于POST方法HTML表单的RestFul WCF服务

时间:2014-11-10 13:03:24

标签: web-services wcf rest

我创建了一个RestFul WCF Web服务并且还部署了它。我可以使用GET方法数据但是我无法使用POST方法数据,它总是向我提供错误的请求响应。

     [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "BookAppointment")]
        AppointmentBookAndCancelResult BookAppointment(AppointmentBookAndCancelInPut input);

这是我的RestFul WCF服务的HTML表单

<form enctype="multipart/form-data" action="http://192.168.15.45:8083/TaqeemServices.svc/BookAppointment" Method="POST">

 <input name="AppointmentNumber" type="text" value="APP-00000003-H042S5"/>
<input name="UserId" type="text" value="4"/>

    <input type="submit" />
</form> 

我总是得到这个错误回复:

<body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p>The server encountered an error processing the request. See server logs for more details.</p>
    </div>
  </body>

有什么问题?

AppointmentBookAndCancelInPut代码:

 [DataContract]
    public class AppointmentBookAndCancelInPut
    {
        string userId = string.Empty;
        string appointmentNumber = string.Empty;

        [DataMember]
        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        [DataMember]
        public string AppointmentNumber
        {
            get { return appointmentNumber; }
            set { appointmentNumber = value; }
        }
    }

根据Shah的说法,我在web.config文件中创建了<serviceDebug includeExceptionDetailInFaults="true"/>,当我运行post方法时,我得到了详细的输出

The server encountered an error processing the request. 
The exception message is 'The incoming message has an unexpected message format 'Raw'. 
The expected message formats for the operation are 'Xml', 'Json'. 
This can be because a WebContentTypeMapper has not been configured on the binding. 
See the documentation of WebContentTypeMapper for more details.'. 
See server logs for more details. The exception stack trace is: 

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

1 个答案:

答案 0 :(得分:0)

WCF Web服务使用可与Json或XML表示一起使用的WebHttpBinding。在你的情况下,你发送的数据是这样的:

-----------------------------7de175231090e00
Content-Disposition: form-data; name="AppointmentNumber"

APP-00000003-H042S5
-----------------------------7de175231090e00
Content-Disposition: form-data; name="UserId"

4 
-----------------------------7de175231090e00--

绝对不是json或xml表示。