首页 > 解决方案 > 无法从 IIS 中托管的网站发出外部请求

问题描述

我有一个 asp.net 核心网站,它从操作方法内部进行外部 HTTP 调用

 public class MyController : Controller
{
    public IActionResult GetData()
    {
        try
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://test.api.somewebsite/data");
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            var resp = webRequest.GetResponse();
            var stream = resp.GetResponseStream();
            byte[] bytes = new byte[10000];
            stream.Read(bytes, 0, bytes.Length);
            return Ok(bytes);
        }
        catch(Exception ex)
        {
            log(ex);
            return null;
        }

    }
}

当我在 IIS-Express 或作为独立控制台 ASP.NET Core 应用程序运行时,一切正常(我从外部网站获得 200 状态)。

当我在本地 IIS(同一台计算机)中托管网站时,出现此异常

System.Net.WebException:发送请求时出错。无法建立与服务器的连接

任何人都知道为什么我无法从 IIS 内部访问该 url?

谢谢

更新实际上我不能从 IIS 内部进行 HTTP 或 HTTPS 调用。

标签: c#iisasp.net-core

解决方案


推荐阅读