.net - 删除期望 : 100-continue from basicHttpBinding

标签 .net wcf http

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

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

最佳答案

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

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

关于.net - 删除期望 : 100-continue from basicHttpBinding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188397/

相关文章:

c# - 如何从其文件夹外执行 .exe

c# - 双击 ListView 中的一行

c# - ServicePointManager.FindServicePoint 的用途是什么?

c# - WCF Rest服务与存储库模式一起使用?

wcf - DataContractSerializer 与 XmlSerializer 之间的区别

python - 从 Django 获取客户端 Active Directory 用户名

.net - 是否可以让两个应用程序在分布式事务中使用相同的数据库而无需实现资源管理器?

c# - 我将如何编写自己的FirstOrDefaultAsync()方法

http覆盖golang中的http头代码而json编码有错误

email - 如何将传入邮件转发到 HTTP 端点?