使用Ocelot API网关时遇Hostname无法解析异常求助
问题排查与解决
错误根源:
异常System.UriFormatException: Invalid URI: The hostname could not be parsed.的直接原因是ocelot.json中第二个路由的DownstreamHostAndPorts配置错误:Host字段填写了带协议前缀的完整地址https://itapsuat.sslcapsec.com,而Ocelot要求Host仅需填写纯主机名(或IP),协议已通过DownstreamScheme指定为https,多余的https://会导致主机名解析失败。修正配置:
将对应路由的Host值修改为纯主机名itapsuat.sslcapsec.com,修正后的配置片段如下:{ "UpstreamPathTemplate": "/gateway/api/V1/SSL/GenrateToken", "UpstreamHttpMethod": [ "Post" ], "DownstreamPathTemplate": "/MasterAPI/api/v1/services/SSL/Token/GenrateToken", "DownstreamScheme": "https", "DownstreamHttpMethod": "POST", "DownstreamHostAndPorts": [ { "Host": "itapsuat.sslcapsec.com", // 去掉https://前缀 "Port": 443 } ], "SwaggerKey": "Token", "RateLimitOptions": { "EnableRateLimiting": true, "Period": "10s", "PeriodTimespan": 10, "Limit": 3, "HttpStatusCode": 429 } }验证步骤:
- 保存修改后的
ocelot.json - 重启Ocelot网关服务
- 通过网关的上游路径
/gateway/api/V1/SSL/GenrateToken发起POST请求,确认是否能正常转发到下游API
- 保存修改后的
内容的提问来源于stack exchange,提问作者Tanmay Nehete
相关产品推荐
相关产品推荐

