gpt4 book ai didi

.net - 在 WCF 中使用 x-www-form-urlencoded Content-Type

转载 作者:行者123 更新时间:2023-12-04 06:23:09 25 4
gpt4 key购买 nike

我正在为 Menalto Gallery 3 编写 .NET 客户端,它使用 RESTful JSON API(参见 API docs)。我决定为我的客户端使用 WCF,它看起来可以大大简化我的工作,这不是因为有一种方法需要 application/x-www-form-urlencoded 的 Content-Type|。而不是 application/json .

我见过各种从 WCF 发送 urlencoded 数据的黑客,例如通过 using a Stream parameter这使我能够发送任意数据,但仍然需要一个 IClientMessageInspector设置内容类型:

internal class GalleryClientMessageInspector : IClientMessageInspector {
public object BeforeSendRequest(ref Message request, IClientChannel channel) {
HttpRequestMessageProperty httpRequestMessage =
getOrAddRequestMessageProperty(request);

if (/* this is the one API method using urlencoded data */) {
httpRequestMessage.Headers["Content-Type"] = "application/x-www-form-urlencoded";
}
}
// ...remaining IClientMessageInspector methods...
}

如您所见,在这种情况下,我的问题是 IClientMessageInspector 不知道消息源自哪个方法(因此我无法查找 UrlEncoded 属性或在这种情况下告诉我使用 urlencoded 格式的内容) .

如何在不诉诸此类黑客的情况下在 WCF 中添加对 urlencoded 消息的支持?

理想情况下,我只想用一个属性来装饰我的方法声明,并将一些检查器、编码器、格式化程序或其他任何东西挂接到 WCF 中,它们会找到这个属性并对方法的参数进行 urlencode,而不是将它们序列化为 JSON,例如。
[
OperationContract,
WebInvoke(UriTemplate = ""),
OverrideMessageFormat(CustomMessageFormat.UrlEncoded) // like this
]
string Login(string user, string password);

最佳答案

默认情况下不支持表单帖子(不支持内容类型),但 WCF 示例提供了两个与此主题相关的示例:

  • 定制 WebContentTypeMapper添加对新内容类型的支持
  • Form Post - HTML 帖子的示例,但您可以检查它是如何工作的,并为 JSON 制作自己的

  • 还有 WCFRestContrib为表单帖子提供支持的项目。旧 Rest Starter Kit还支持表单帖子,但 REST Starter Kit 从未通过社区预览。对于最后一个即将到来的 Web-API (它将成为 future WCF 版本的一部分)也支持使用表单。目前,Web-API 可作为 CTP1 使用。

    关于.net - 在 WCF 中使用 x-www-form-urlencoded Content-Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6328972/

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