You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

更换硬盘重装Win11后调用外部SOAP服务报Content-Type缺失错误

解决Windows 11 23H2下.NET 4.6.2调用SOAP服务报Content-Type缺失问题
  • 显式强制添加Content-Type请求头
    新系统下.NET Framework的默认请求头生成逻辑可能有变更,别依赖框架自动添加,手动在SOAP客户端请求前注入头部:

    // 实例化SOAP客户端
    var soapClient = new YourSoapServiceClient();
    // 添加自定义头部行为
    soapClient.Endpoint.Behaviors.Add(new ForceContentTypeBehavior());
    
    // 自定义行为实现类
    public class ForceContentTypeBehavior : IEndpointBehavior
    {
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.ClientMessageInspectors.Add(new ContentTypeInspector());
        }
    
        // 以下为接口空实现,无需修改
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
        public void Validate(ServiceEndpoint endpoint) { }
    }
    
    public class ContentTypeInspector : IClientMessageInspector
    {
        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            var httpRequestProp = new HttpRequestMessageProperty();
            // SOAP 1.1用text/xml,SOAP 1.2用application/soap+xml,根据实际服务调整
            httpRequestProp.Headers["Content-Type"] = "text/xml;charset=utf-8";
            request.Properties[HttpRequestMessageProperty.Name] = httpRequestProp;
            return null;
        }
    
        public void AfterReceiveReply(ref Message reply, object correlationState) { }
    }
    
  • 更新.NET Framework 4.6.2补丁并设置兼容性模式

    1. 打开Windows更新,检查并安装.NET Framework 4.6.2的最新可选补丁;
    2. 找到项目输出的exe文件,右键→属性→兼容性,勾选“以兼容模式运行这个程序”,选择Windows 10,保存后测试。
  • 检查并修复TLS相关注册表配置
    确认以下注册表项是否正确设置(若不存在则新建):

    • 路径:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319,新建DWORD值SchUseStrongCrypto,赋值为1;
    • 路径:HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319,同样新建SchUseStrongCrypto,赋值为1;
      设置完成后重启电脑,再测试SOAP调用。
  • 抓包对比请求差异
    用Fiddler或Wireshark抓取新旧系统下的HTTP请求包,对比Content-Type头是否存在、其他请求头(如User-Agent、Accept)是否有差异,根据差异针对性调整客户端配置。

  • 重新生成SOAP客户端代理
    新系统下的服务引用生成工具可能有细微变化,尝试用Visual Studio的“添加服务引用”或svcutil.exe重新生成SOAP客户端代理类,替换原有代码后测试。

内容的提问来源于stack exchange,提问作者dli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 07:47:09