客户端在调用我们的WS时拒绝添加Content-Type
。
我尝试了this question中建议的方法,虽然它没有用。
public class ConTypeFilter:DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if ( request.Content.Headers.ContentType==null)
{
request.Content.Headers.ContentType=new MediaTypeHeaderValue("application/json");
}
return base.SendAsync(request, cancellationToken);
}
}
是否有其他方法可以拦截请求并为入站请求添加标头?
我想要修改它的原因是因为SOAP WS删除了内容类型不同于text/xml
或application/soap+xml
的消息,而用户表示他们将发送application/x-www-form-urlencoded
并拒绝更改。
答案 0 :(得分:0)
拦截来自客户端的消息的其他方法是通过方法之前和之后:
#region IClientMessageInspector Members
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
Console.WriteLine("IClientMessageInspector.AfterReceiveReply called.");
Console.WriteLine("Message: {0}", reply.ToString());
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)
{
Console.WriteLine("IClientMessageInspector.BeforeSendRequest called.");
return null;
}
有关详细信息,请参阅链接here。