gpt4 book ai didi

c# - 使用 POST(json 数据)调用 REST 服务方法时出现错误请求错误?

转载 作者:行者123 更新时间:2023-11-30 17:50:50 25 4
gpt4 key购买 nike

您好,我是 RESTful WCF 的新手,我正在尝试使用 POST 对 web 服务方法进行简单调用,这是我的代码

Service Interface code

[ServiceContract]
public interface IJsonSave
{

[OperationContract]
[WebInvoke(UriTemplate = "/SaveJason", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
string SaveJason(string value);
}

web.config文件代码

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="servicebehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="JsonDataSave.JsonSave"
behaviorConfiguration ="servicebehavior" >
<endpoint name ="SOAPEndPoint"
contract ="JsonDataSave.IJsonSave"
binding ="basicHttpBinding"
address ="soap" />
<endpoint name ="RESTEndPoint"
contract ="JsonDataSave.IJsonSave"
binding ="webHttpBinding"
address ="Rest"
behaviorConfiguration ="restbehavior" />
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

我尝试使用 fiddler 她是我的标题

POST http://localhost:50267/JsonSave.svc/Rest/SaveJason
User-Agent: Fiddler
Content-Type: application/json
Data-Type: json
Host: localhost:50267

请求正文:

 ({"value":"asdfasd"})

它给出了 HTTP/1.1 400 Bad Request 的错误,并且在启用调试细节时它给出了以下堆栈跟踪

The server encountered an error processing the request. The exception message is 'Error    in deserializing body of request message for operation 'SaveJason'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).'. See server logs for more details. The exception stack trace is: 
at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters) 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)

我在谷歌上搜索了很多并尝试了所有可用的解决方案,但它仍然没有工作任何帮助都会很棒。它适用于 SOAP 只是给 REST 错误!

最佳答案

为了收集有关数据流和错误位置的其他信息,您可能会受益于在服务端启用 WCF 跟踪。以下链接应提供足够的详细信息:http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

关于您的场景中的错误,我怀疑客户端和服务的消息编码设置不一致。我想知道是否可能:

BodyStyle = WebMessageBodyStyle.WrappedRequest

应该是:

BodyStyle = WebMessageBodyStyle.Wrapped

BodyStyle = WebMessageBodyStyle.Bare

因此请求和响应具有相同的包装样式。

问候,

关于c# - 使用 POST(json 数据)调用 REST 服务方法时出现错误请求错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20325516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com