【问题标题】:SocketException (0x274c) while accessing API via System.Net.Http.HttpClient通过 System.Net.Http.HttpClient 访问 API 时出现 SocketException (0x274c)
【发布时间】:2023-03-07 20:43:01
【问题描述】:

我正在开发一个 ASP.NET MVC 应用程序。我在那里通过System.Net.Http.HttpClient 使用API​​ 服务。这是执行工作的控制器

        private readonly HttpClient _client;
        public HomeController()
        {
          _client = new HttpClient()
        }

        public async Task<ActionResult> Index(SearchParam searchParam)
        {
            // API Initialization
            _client.DefaultRequestHeaders.Clear();
            _client.BaseAddress = new Uri(Uri);
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            // API Consumption By HttpClinet
            var url = _searchService.GetApiQueryParams(searchParam);
            var response = await _client.GetAsync(url);

            var jsonString = await response.Content.ReadAsStringAsync();
            var rootobject = JsonConvert.DeserializeObject<Response.Rootobject>(jsonString);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                var searchResults = _searchService.GetSearchResults(rootobject, searchParam);
                return View("SearchResult", searchResults);
            }

            ModelState.AddModelError(string.Empty, "Sorry some internal error occur !");
            return View("SearchResult");
        }

这在我的Local ServerIIS Server 上运行良好。但是当我将应用程序发布到不同的托管服务器并尝试使用 API 时,它会显示 this 错误。

我发现了两个类似的问题。请看看这些

从第一个问题开始,我没有得到“负载均衡器到服务器”。 从第二个问题开始,当我在Web.config 添加解决方案时,它显示proxyaddress="http://proxyserver" 的错误是有道理的。

现在我急需解决这个问题。本人不擅长追问,如有错误欢迎指正或指正。

【问题讨论】:

  • 您是否尝试从托管应用程序的位置远程登录 API 服务器 IP/端口?如果您无法使用 telnet 进行连接,则可能是一些防火墙问题。
  • 其实我对应用层的telnet一无所知

标签: c# asp.net-mvc server


【解决方案1】:

终于,问题解决了。提到second issue,我将问题通过电子邮件发送给了托管服务提供商,他们提供了system.netconfiguration。在Web.config 中使用这个system.net configuration 解决了这个问题。

<system.net>
 <defaultProxy>
  <proxy
    usesystemdefault = "false"
    bypassonlocal="false"
    proxyaddress="http://hosting-provided-proxyaddress-here"
  />
 </defaultProxy>
</system.net>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    相关资源
    最近更新 更多