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

本地运行ASP.NET Core调用Azure Computer Vision时遇HTTP请求未知错误

ASP.NET Core本地调用Azure Computer Vision报错,curl请求正常的排查方案

我创建了一个ASP.NET Core Web API项目,用于和Azure Computer Vision实例通信生成图像标题。使用密钥凭据完成认证(后续计划切换为托管标识),代码部署到Azure App Service后运行完全正常,但本地运行时出现错误,而用curl直接从本地请求Azure Computer Vision实例却没有问题。

调用代码(C#)

var serviceOptions = new VisionServiceOptions(
    "https://NAME.cognitiveservices.azure.com/",
    new AzureKeyCredentials("KEY"));

using var imageSource = VisionSource.FromUrl(
     new Uri("https://learn.microsoft.com/azure/cognitive-services/computer-vision/media/quickstarts/presentation.png"));

var analysisOptions = new ImageAnalysisOptions()
{
    Features =
          ImageAnalysisFeature.CropSuggestions
        | ImageAnalysisFeature.Caption
        | ImageAnalysisFeature.DenseCaptions
        | ImageAnalysisFeature.Objects
        | ImageAnalysisFeature.People
        | ImageAnalysisFeature.Text
        | ImageAnalysisFeature.Tags
};

var analyzer = new ImageAnalyzer(serviceOptions, imageSource, analysisOptions);

var result = analyzer.Analyze();

if (result.Reason == ImageAnalysisResultReason.Analyzed)
{
    return Ok(result);
}

return NotFound(ImageAnalysisErrorDetails.FromResult(result));

本地运行错误信息

{
  "message": "Unknown error in sending http request",
  "errorCode": -1,
  "reason": 5
}

正常工作的curl命令

curl -H "Ocp-Apim-Subscription-Key: KEY" -H "Content-Type: application/json" "https://NAME.cognitiveservices.azure.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2023-02-01-preview" -d "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"

排查与解决建议

  • 检查本地代理配置:如果本地走代理,SDK可能未自动获取代理设置。可以手动为HttpClient指定代理:
    var handler = new HttpClientHandler
    {
        UseProxy = true,
        Proxy = new WebProxy("http://你的代理地址:端口")
    };
    var httpClient = new HttpClient(handler);
    var analyzer = new ImageAnalyzer(serviceOptions, imageSource, analysisOptions, httpClient);
    
  • 更新SDK版本:确认Azure.AI.Vision.ImageAnalysis SDK为最新稳定版,旧版本可能存在本地网络适配问题,通过NuGet更新即可。
  • 排查防火墙/安全软件:本地防火墙或杀毒软件可能拦截了SDK的HTTPS请求,可暂时关闭测试,若恢复正常则添加放行规则。
  • 指定TLS版本:尝试强制使用TLS 1.2发起请求,避免版本兼容问题:
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    
  • 核对本地配置:检查本地开发环境配置文件(如appsettings.Development.json)中的端点URL和密钥是否与curl中的完全一致,排除配置错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 13:51:13