13

如果客户端以不支持的媒体类型向 HTTP 服务器发送数据,服务器会以“ 415 unsupported media type ”状态回复。但是如何告诉客户端支持哪些媒体类型呢?是否有标准或至少推荐的方法?还是只是将其作为文本写入响应正文?

4

4 回答 4

9

在这种情况下,根本没有关于做什么的规范,所以希望实现无处不在。(如果服务器的响应包含诸如标头之类的东西,那将是明智的,Accept:因为它具有几乎正确的语义,如果当前方向错误。)

于 2010-07-27T14:43:51.153 回答
0

I believe you can do this with the OPTIONS Http verb.

Also the status code of 300 Multiple Choices could be used if your scenario fits a certain use case. If they send a request with an Accept header of application/xml and you only support text/plain and that representation lives at a distinct URL then you can respond with a 300 and in the Location header the URL of that representation. I realize this might not exactly fit your question, but it's another possible option.

And from the HTTP Spec:

10.4.7 406 Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

Unless it was a HEAD request, the response SHOULD include an entity containing a list of available entity characteristics and location(s) from which the user or user agent can choose the one most appropriate. The entity format is specified by the media type given in the Content-Type header field. Depending upon the format and the capabilities of the user agent, selection of the most appropriate choice MAY be performed automatically. However, this specification does not define any standard for such automatic selection.

      Note: HTTP/1.1 servers are allowed to return responses which are
      not acceptable according to the accept headers sent in the
      request. In some cases, this may even be preferable to sending a
      406 response. User agents are encouraged to inspect the headers of
      an incoming response to determine if it is acceptable.
于 2010-07-27T14:30:43.113 回答
0

tl;博士; 将生成的代理类编辑为继承自Microsoft.Web.Services3.WebServicesClientProtocol**。

我在解决这个错误时遇到了这个问题,所以我想我会帮助下一个可能会来到这里的人,尽管不确定它是否回答了所述的问题。当我不得不接管一个使用 WSE 和 MTOM 编码的现有解决方案时,我遇到了这个错误。这是一个调用 Web 服务的 Windows 客户端。

到目前为止,客户端正在调用 Web 服务,它会抛出该错误。对我来说有助于解决该错误的事情是检查显然默认生成的 Web 服务代理类继承自System.Web.Services.Protocols.SoapHttpClientProtocol. 从本质上讲,这意味着它实际上并未使用 WSE3。

无论如何,我手动编辑了代理并将其更改为继承自Microsoft.Web.Services3.WebServicesClientProtocol.

顺便说一句,要在 VS 中查看生成的代理类,请单击 Web 参考,然后单击“显示所有文件”工具栏按钮。reference.cs 是一个快乐的地方!

希望能帮助到你。

于 2014-10-01T14:53:49.123 回答
-3

在他的书“HTTP Developer's Handbook”(第 81 页)中,Chris Shiflett 解释了 415 的含义,然后他说:“HTTP 响应内容中使用的媒体类型应该在 Content-Type 实体标头中指示。”

1) 那么 Content-Type 是一个可能的答案吗?它可能是接受的内容类型的逗号分隔列表。这种可能性的明显问题是 Content-Type 是实体标头而不是响应标头。

2)或者这是书中的错字?他真的是想说“HTTP 请求”吗?

于 2011-12-12T20:21:49.513 回答