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

.NET Core 2.0 SOAP客户端调用报错:WinHttpException: 操作超时

解决.NET Core 2.0 SOAP客户端调用超时问题

我之前在调用公共SOAP服务时也碰到过类似的超时问题,结合你的场景,给你几个实用的排查和解决方向:

1. 修正Endpoint地址(最常见的问题)

你当前使用的Endpoint地址是带?WSDL的WSDL文档地址,这是用来生成服务代理的,实际调用服务时需要使用服务的真实端点地址,也就是去掉?WSDL部分:

var ep = new EndpointAddress("http://www.dneonline.com/calculator.asmx");

2. 调整绑定的超时参数

.NET Core的BasicHttpBinding默认超时时间比较短,网络波动时可能不够用,你可以手动延长相关超时设置:

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
// 根据实际情况调整各类超时时间
binding.OpenTimeout = TimeSpan.FromSeconds(30);
binding.SendTimeout = TimeSpan.FromSeconds(30);
binding.ReceiveTimeout = TimeSpan.FromSeconds(30);

3. 检查网络环境

  • 先在浏览器中直接访问http://www.dneonline.com/calculator.asmx,确认你的网络能正常连通这个服务,排除防火墙、代理或内网限制的问题。
  • 如果是公司内网环境,可能需要在代码中配置代理:
    var handler = new HttpClientHandler
    {
        Proxy = new WebProxy("http://你的代理地址:端口"),
        UseProxy = true
    };
    // 将handler传入客户端构造函数(如果生成的代理支持的话)
    Calculator.CalculatorSoapClient client = new Calculator.CalculatorSoapClient(binding, ep, handler);
    

4. 验证服务可用性

公共测试服务偶尔可能会临时不可用,你可以用SOAPUI工具导入该WSDL,测试Add/Subtract等方法是否正常响应,排除服务端本身的问题。

修正后的完整调用示例

var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
// 配置超时
binding.OpenTimeout = TimeSpan.FromSeconds(30);
binding.SendTimeout = TimeSpan.FromSeconds(30);
binding.ReceiveTimeout = TimeSpan.FromSeconds(30);

// 使用正确的端点地址
var ep = new EndpointAddress("http://www.dneonline.com/calculator.asmx");

// 初始化客户端(using确保资源释放)
using (var client = new Calculator.CalculatorSoapClient(binding, ep))
{
    try
    {
        // 推荐用异步调用,避免同步阻塞导致的超时
        var addResult = await client.AddAsync(5, 3);
        Console.WriteLine($"计算结果:{addResult}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"调用出错:{ex.Message}");
    }
}

另外注意,.NET Core 2.0对SOAP客户端的支持相对基础,确保你的服务代理类是通过Visual Studio的"连接服务"正确生成的,没有遗漏依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:05:37