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

Azure App Service中ASP.NET应用调用Web服务时HttpClient.SendAsync报错

问题描述

部署在Azure App Service中的ASP.NET(.NET Framework 4.8)应用,使用HttpClient发起POST调用外部API时,在httpclient.SendAsync(request)处抛出通用错误:"An error occurred while sending the request"。该应用在本地开发环境、Azure虚拟机中均可正常运行,已调整App Service的HTTP版本、TLS版本、.NET版本等配置,仍无法解决,推测是App Service环境的握手或网络限制导致。

相关代码如下:

public async Task<T> MakeWebServiceCall<T>(String my_input_data, Uri webServiceUri)
{
    HttpRequestMessage request = new HttpRequestMessage();
    request = new HttpRequestMessage(HttpMethod.Post, webServiceUri);
    request.Headers.Add("Accept", "*/*");
    var collection = new List<KeyValuePair<string, string>>();
    string paramvalue = System.Net.WebUtility.UrlDecode(my_input_data);
    collection.Add(new KeyValuePair<string, string>("data", paramvalue));
    var content = new FormUrlEncodedContent(collection);
    request.Content = content;
    request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
    HttpMessageHandler handler = new HttpClientHandler
    {
        SslProtocols = System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
    };
    HttpClient httpclient = new HttpClient(handler);
    httpclient.Timeout = new TimeSpan(0,0,5);
    var response_result = await httpclient.SendAsync(request); // *** 报错行 ***
    response_result.EnsureSuccessStatusCode();
    var response_result_final = await  response_result.Content.ReadAsStringAsync();
            
    JsonSerializerSettings JSONFormatSettings = new JsonSerializerSettings();
    JSONFormatSettings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
    T result = JsonConvert.DeserializeObject<T>(response_result_final, JSONFormatSettings);
    return result;
}
可能的解决方案及排查方向
  • 捕获更详细的错误信息:通用错误无法定位根因,在SendAsync的try-catch中捕获HttpRequestException的内部异常,记录完整错误栈和InnerException信息(比如写入App Service日志流),常见深层原因包括SSL握手失败、代理冲突、端口限制等。示例代码:

    try
    {
        var response_result = await httpclient.SendAsync(request);
        response_result.EnsureSuccessStatusCode();
        // 后续逻辑
    }
    catch (HttpRequestException ex)
    {
        // 记录ex.Message、ex.InnerException?.Message、ex.StackTrace
        throw;
    }
    
  • 检查出站网络限制:Azure App Service对出站端口有默认限制,若外部API使用非标准端口,需确认该端口在App Service允许的出站端口列表中;若外部API部署在私有网络,需配置App Service的VNet集成以打通网络。

  • 调整SSL/TLS配置:

    • 登录Azure门户,进入App Service的设置 > TLS/SSL设置,将最低TLS版本设为1.2(与外部API支持版本匹配);
    • 尝试移除代码中SslProtocols的显式设置,让系统自动协商最优协议,避免手动指定的协议与环境冲突。
  • 排查代理配置:App Service默认通过内部代理处理出站请求,可尝试在HttpClientHandler中禁用自动代理,或配置特定代理(若外部API需要):

    HttpMessageHandler handler = new HttpClientHandler
    {
        UseProxy = false,
        SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
        ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
    };
    
  • 优化HttpClient实例化:代码中每次调用创建新HttpClient实例,易导致连接耗尽,在App Service环境中更易触发问题。建议使用单例模式复用HttpClient:

    private static readonly HttpClient _httpClient;
    
    static YourClassName()
    {
        var handler = new HttpClientHandler
        {
            SslProtocols = System.Security.Authentication.SslProtocols.Tls12,
            ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true,
        };
        _httpClient = new HttpClient(handler);
        _httpClient.Timeout = new TimeSpan(0, 0, 5);
    }
    
    public async Task<T> MakeWebServiceCall<T>(String my_input_data, Uri webServiceUri)
    {
        // 构建请求逻辑
        var response_result = await _httpClient.SendAsync(request);
        // 后续处理
    }
    
  • 验证证书信任:虽然代码禁用了证书验证,但App Service的根证书存储与虚拟机不同,可尝试启用验证排查是否为根证书未信任问题,或确认外部API证书在App Service信任列表中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 03:05:26