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

使用HttpClient调用非443端口REST接口遇访问拒绝问题求助

解决HttpClient调用非443端口HTTPS接口出现Access Denied的问题

问题场景

在Windows 10 + Visual Studio 2019环境下,使用以下C#代码调用运行在9443端口的HTTPS REST接口时:

using (var httpClient = new HttpClient())
{
    string url = "https://myrestapi.company.com:9443/api/healthcheck";
    using (var response = await httpClient.GetAsync(url))
    {
        string apiResponse = await response.Content.ReadAsStringAsync();
        Console.WriteLine(apiResponse);
    }
}

控制台程序(无论在VS中运行还是直接执行exe)均返回错误:

Access Denied (connect_method_denied)

return (<FONT face="Helvetica">
Your request attempted a CONNECT to a port "9443" that is not permitted by default.
</FONT>)

This is typically caused by an HTTPS URL that uses a port other then the default of 443.

但同一机器上通过Swagger或Git Bash执行curl命令可正常访问:

curl -X GET --header 'Accept: application/json' 'https://myrestapi.company.com:9443/api/healthcheck'

解决方案

1. 调整HttpClient的代理配置

HttpClient默认会使用系统代理,而你的代理服务器大概率限制了非443端口的CONNECT请求。可以通过以下两种方式处理:

  • 禁用代理:强制HttpClient不使用任何代理,直接发起请求:
    var handler = new HttpClientHandler
    {
        UseProxy = false
    };
    using (var httpClient = new HttpClient(handler))
    {
        string url = "https://myrestapi.company.com:9443/api/healthcheck";
        using (var response = await httpClient.GetAsync(url))
        {
            string apiResponse = await response.Content.ReadAsStringAsync();
            Console.WriteLine(apiResponse);
        }
    }
    
  • 指定允许9443端口的代理:如果必须通过代理访问,需要在代理服务器中添加允许9443端口的CONNECT请求规则,或者在代码中配置对应代理:
    var handler = new HttpClientHandler
    {
        Proxy = new WebProxy("http://your-proxy-address:port")
        {
            BypassProxyOnLocal = false
        },
        UseProxy = true
    };
    using (var httpClient = new HttpClient(handler))
    {
        // 后续请求代码不变
    }
    

2. 检查系统代理设置

打开Windows「设置」→「网络和Internet」→「代理」,查看是否启用了自动检测代理或手动配置的代理。可以临时关闭代理后重新运行程序,验证是否是代理导致的限制。

3. 对齐curl与HttpClient的代理策略

执行curl -v https://myrestapi.company.com:9443/api/healthcheck查看curl实际使用的代理情况:

  • 如果curl未使用代理,那么禁用HttpClient的代理即可解决问题;
  • 如果curl使用了特定代理,将HttpClient的代理配置调整为一致即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:57:33