修改传入SOAP请求的Content-Type

时间:2015-08-17 06:10:22

标签: c# soap

客户端在调用我们的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/xmlapplication/soap+xml的消息,而用户表示他们将发送application/x-www-form-urlencoded并拒绝更改。

1 个答案:

答案 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