删除Expect:100-continue from basicHttpBinding

时间:2014-03-05 04:15:35

标签: .net wcf http

我想删除由BasicHttpBinding中使用的底层HttpWebRequest添加的Expect:100-continue标头。我知道只需将ServicePointManager.Expect100Continue设置为false即可。但是,这种方法的问题在于它是全局的,即它适用于在该过程中发起的所有Web请求。我想将此范围限制为特定的WCF代理。使用ASMX代理这很容易 - 我只是将生成的代理子类化,这是SoapHttpClientProtocol的子类并覆盖GetWebRequest。然后,我将调用基本实现并在返回的Web请求对象上设置Expect100Continue。

我正在尝试对WCF应用类似的方法,但是无法找到一种方法来“拦截”传输通道创建的HttpWebRequest。这可能吗?

2 个答案:

答案 0 :(得分:0)

您可以在代码中为特定端点执行此操作,如下所示:

System.Net.ServicePoint servicePoint =
     System.Net.ServicePointManager.FindServicePoint(myWcfService.Endpoint.Address.Uri);

servicePoint.Expect100Continue = false;
// now execute some service operation 

我认为不可能通过配置来实现。

答案 1 :(得分:0)

对于System.ServiceModel.Http 4.5,我们可以创建一个自定义BasicHttpBinding并在HttpMessageHandler管道中注入HttpClient

class CustomHttpBinding: BasicHttpBinding
{
    public override BindingElementCollection CreateBindingElements()
    {
        var elements =  base.CreateBindingElements();
        var transport = elements.Find<HttpsTransportBindingElement>();
        if (transport != null)
        {
            elements.Remove(transport);
            elements.Add(CustomHttpsTransportBindingElement.CreateFromHttpsTransportBindingElement(transport));
        }
        return elements;
    }       
}

class CustomHttpsTransportBindingElement: HttpsTransportBindingElement
{
    private Func<HttpClientHandler, HttpMessageHandler> _createDelegatingHandler = client => new CustomHttpMessageHandler(client);
    public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
    {            
        context.BindingParameters.Add(_createDelegatingHandler);
        return base.BuildChannelFactory<TChannel>(context);
    }        

    public override BindingElement Clone()
    {
        return CreateFromHttpsTransportBindingElement(this);
    }

    public static CustomHttpsTransportBindingElement CreateFromHttpsTransportBindingElement(HttpsTransportBindingElement from)
    {
        return new CustomHttpsTransportBindingElement
        {
            AllowCookies = from.AllowCookies,
            AuthenticationScheme = from.AuthenticationScheme,
            BypassProxyOnLocal = from.BypassProxyOnLocal,
            ManualAddressing = from.ManualAddressing,
            MaxBufferSize = from.MaxBufferSize,
            MaxReceivedMessageSize = from.MaxReceivedMessageSize,
            ProxyAddress = from.ProxyAddress,
            ProxyAuthenticationScheme = from.ProxyAuthenticationScheme,
            RequireClientCertificate = from.RequireClientCertificate,
            TransferMode = from.TransferMode,
            UseDefaultWebProxy = from.UseDefaultWebProxy,
            WebSocketSettings = from.WebSocketSettings
        };
    }
}

class CustomHttpMessageHandler: DelegatingHandler
{
    public CustomHttpMessageHandler(HttpMessageHandler innerHandler): base(innerHandler)
    {            
    }
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        request.Headers.ExpectContinue = false;
        return base.SendAsync(request, cancellationToken);
    }
}

如果您不使用HTTPS,请覆盖HttpTransportBindingElement而不是HttpsTransportBindingElement