【问题标题】:Fiddler POST Invalid Header NameFiddler POST 标题名称无效
【发布时间】:2014-09-16 06:42:07
【问题描述】:

我正在使用 fiddler 向 WCF Web 服务发送 POST 请求,该服务以 HTTP 400 响应:请求的标头名称无效。 发布请求如下所示:

User-Agent: Fiddler
Host: localhost:49392
Content-Type: application/json
Content-Length: 0
{  "clientFirstName" : "John"}

我的端点定义如下:

 [OperationContract]
 [System.ServiceModel.Web.WebInvoke
       (Method = "POST",
       RequestFormat = WebMessageFormat.Json,
       ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json,
       BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.Wrapped,
       UriTemplate = "MakeReservation")]
 String MakeReservation(Stream reservationStream);

更多

首先,JSON 在错误的窗格中。我将其移至“请求正文”窗格。

我更改了函数的原型,以使用 String 而不是 Stream 作为输入参数。该服务现在接受我的调用并返回 200,但在调试器中我看到输入参数 String 为空。当我将它改回 Stream 时,我又得到了 400。

还有更多

启用跟踪在跟踪文件中为我提供以下消息:

Incoming message for operation 'MakeReservation' (contract 'ITalLimoService' with namespace 
'http://tempuri.org/') contains an unrecognized http body format value 'Json'. The expected body 
format value is 'Raw'. This can be because a WebContentTypeMapper has not been configured on the 
binding. See the documentation of WebContentTypeMapper for more details.

如 WebInvoke 定义中所述,RequestFormat 和 ResponseFormat 都设置为 WebMessageFormate.Json。为什么 IIS 抱怨这个?

【问题讨论】:

  • 您的第一个块中的数据格式错误;您在标题中显示 JSON 文本,并且当您清楚地发送内容时,您有一个 Content-Length: 0。请更新您的问题,以便我们准确了解您在做什么。

标签: wcf rest fiddler


【解决方案1】:

我不知道为什么会这样,但出于某种原因,从提琴手 Composer 中删除以下行就可以了:

Content-Type: application/json

就是这样。我会给出解释的分数。

【讨论】:

  • 错误信息似乎准确地解释了这个问题:This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.
【解决方案2】:

我知道这篇文章已经过时了。但是很奇怪,当我删除标题“application/json”时它起作用了。

它与服务器设置有关吗?

【讨论】:

    【解决方案3】:

    由于语法变形,服务器无法理解请求。客户端不应该不加修改地重复请求。如果向您的服务器请求的数据格式正确,但数据不正确。例如:一个 Posted String 值到一个期望 String 值的 API 端点;但是,字符串的值包含其他数据。

    请求实体的语法正确(因此 400(错误请求)状态代码不合适)但无法处理包含的指令。例如,如果 XML 请求正文包含语法正确但语义错误的 XML 指令,则可能会出现此错误情况。

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2019-10-08
      • 2016-03-26
      相关资源
      最近更新 更多