REST 服务上的错误请求错误 使用 POST(json 数据)调用方法?

2024-04-30

您好,我是 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>

我尝试使用提琴手她是我的标题

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 http://msdn.microsoft.com/en-us/library/ms733025(v=vs.110).aspx

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

BodyStyle = WebMessageBodyStyle.WrappedRequest

应该是:

BodyStyle = WebMessageBodyStyle.Wrapped

or

BodyStyle = WebMessageBodyStyle.Bare

所以请求和响应具有相同的包装风格。

Regards,

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

REST 服务上的错误请求错误 使用 POST(json 数据)调用方法? 的相关文章

随机推荐

  • 在日期 presto SQL 上运行总和

    我正在尝试使用 Presto SQL 根据下面的示例数据计算某个日期内 t 列和 s 列的累积总和 Date T S 1 2 19 2 5 2 1 19 5 1 3 1 19 1 1 我想得到 Date T S cum T cum S 1
  • ReDim 在 VB6 中保留为多维数组

    我正在使用 VB6 我需要做一个ReDim Preserve到多维数组 Dim n m As Integer n 1 m 0 Dim arrCity As String ReDim arrCity n m n n 1 m m 1 ReDim
  • 如何使用新的 html 文件打开更新电子浏览器窗口

    我是电子新手 我有两个 html 页面 我想在单击按钮时打开第二个页面 我的代码如下 但我只是得到一个空白窗口 不是第二页 这是index js const electron require electron const app Brows
  • 仅当存在 2 个类时,才能使用 CSS 定位元素吗?

    您可能已经知道 元素上可能有多个类 并用空格分隔 Example div class content main div 借助 CSS 您可以瞄准这一点div与任一 content or main 有没有办法瞄准它 如果并且仅当两个班都在吗
  • AspectJ - 匹配具有通用参数的方法的切入点

    我有一个接受任何类型作为其参数的通用方法 例如 我想要一个切入点 它与仅以 String 类型作为参数的方法的调用相匹配 最终的要求是将建议执行的范围限制为 字符串 参数 这是我的通用类和方法 public class Param
  • UIModalTransitionStylePartialCurl 在 iOS 8 上消失

    我试图向我的 ViewController 展示 UIModalTransitionStylePartialCurl 转换 我正在使用以下代码 PuzzleViewController vc PuzzleViewController all
  • Pygame:居中文本系统字体文本

    我读过这篇关于居中文本的文章 Pygame 如何使文本居中 https stackoverflow com questions 23982907 python library pygame centering text 但是 不是从文件导入
  • 开发目录结构[关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 使用 iPhone 版 gmap 中的经纬度计算两个地点之间的距离 [重复]

    这个问题在这里已经有答案了 可能的重复 GPS 坐标 以度为单位 来计算距离 https stackoverflow com questions 6994101 gps coordinates in degrees to calculate
  • 获取 iPhone 文档目录。 NSSearchPathForDirectoriesInDomains 仍然是唯一的方法吗?

    Is the NSSearchPathForDirectoriesInDomainss 函数仍然是获取 iPhone Documents 目录路径的最佳方法吗 我之所以这么问 是因为我看到的大多数主题都是去年的 而且这似乎仍然是获取 iPh
  • 将 Django/Python 3.4 部署到 Heroku

    我正在尝试使用 Django Heroku 入门教程使用 Django Heroku 部署我的第一个示例应用程序 我的工具 Python 3 4 和 Windows 7 PowerShell 我的挑战 部署到 Heroku 失败 我不知道为
  • App.config 连接字符串相对路径

    我需要在 app config 中设置 sqlite 连接字符串 我想设置相对于调试 发布文件夹的路径 数据库文件将复制到这些文件夹
  • Windows命令行参数编码是什么?

    Windows 使用什么编码来传递给在 cmd exe 窗口中启动的程序的命令行参数 命令行参数的编码似乎不受使用控制台代码页设置的影响chcp 我将其设置为 UTF 8 代码页 65001 并使用 Lucida Console 字体 如果
  • Python 中的静态变量?

    在 C 中 我们有 static 关键字 它在循环中是这样的 for int x 0 x lt 10 x for int y 0 y lt 10 y static int number of times 0 number of times
  • 不循环查找

    我有两个表 我正在尝试从一个表中查找值以添加到另一个表中的值 目前我正在使用两个 for 循环 但它们运行缓慢 我是 R 新手 知道我应该避免循环以加快速度 但我不知道如何做 表1 几千行 37列 type cat1 cat2 cat3 c
  • C++ 的异步函数调用

    我需要提示如何在 C C 中实现异步函数调用 或 Windows 和 或 Linux 的框架 API 调用的名称 用例如下 父线程调用函数 该函数创建一个子线程并返回 因此调用是非阻塞的 父线程可以继续执行某些工作 例如 pthread j
  • React PropTypes:数字范围

    有没有更好的方法来验证如果数字在范围内 避免写作 PropTypes oneOf 1 2 3 4 5 6 7 8 9 10 根据文档 https reactjs org docs typechecking with proptypes ht
  • Sentry django 配置 - 记录器

    我正在尝试使用简单的日志记录并希望将错误 异常发送到 Sentry 我按照文档配置了 Sentry 并在我的开发人员上成功运行了测试 python manage py raven test 我添加了日志记录配置 如下所示哨兵文档 https
  • 使用 Struts 2 的 RESTful Web 服务

    我们正在评估一种教育活动设计 我们希望以 RESTful Web 服务的形式创建完整的后端 处理业务逻辑 这些服务可供各种应用程序使用 从 Struts 2 框架调用这些 Web 服务是个好主意吗 我读了几篇文档 但人们不鼓励这样做 我们很
  • REST 服务上的错误请求错误 使用 POST(json 数据)调用方法?

    您好 我是 RESTful WCF 的新手 我正在尝试使用 POST 对 Web 服务方法进行简单调用 这是我的代码 Service Interface code ServiceContract public interface IJson