在WCF中使用x-www-form-urlencoded Content-Type
||
我正在为Menalto Gallery 3编写.NET客户端,该客户端使用RESTful JSON API(请参阅API文档)。我决定将WCF用于我的客户端,这似乎可以大大简化我的工作,除非不是因为有一种方法要求Content-Type为
application/x-www-form-urlencoded
而不是application/json
。
我见过各种从WCF发送urlencoded数据的技巧,例如通过使用Stream参数,该参数使我能够发送任意数据,但仍然需要2来设置Content-Type:
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中,WCF会找到该属性并对该方法的参数进行urlencode,而不是将它们序列化为JSON。
[
OperationContract,
WebInvoke(UriTemplate = \"\"),
OverrideMessageFormat(CustomMessageFormat.UrlEncoded) // like this
]
string Login(string user, string password);
没有找到相关结果
已邀请:
1 个回复
街茬