尝试请求 API 时连接失败,仅当在 Ionos/1&1 主机上部署到生产环境时

Connection failed when trying to request an API, only when deploying into production on Ionos/1&1 Hosting

我正在用 C#/.NET 创建一个小型网站 api,它有时必须从外部请求一些数据 API。 当我在本地测试时,在我的机器上,它运行完美。

但是当它部署在远程网络托管上时(我正在使用 Ionos/1&1 Web Windows 托管解决方案),为了测试我的应用程序,我从我自己的 API:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond {ip}

这是调用 API 并获得响应的代码

        Uri myUri = new Uri("url", UriKind.Absolute);
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(stream))
            {
                string result = reader.ReadToEnd();
                return JsonConvert.DeserializeObject<ModelOfResult>(result);
            }
        }

是代理问题吗?代码问题?托管问题?

抱歉我的英语不好,提前谢谢你

看来是代理问题。请尝试将此代码添加到您的 web.config:

 <system.net>
    <defaultProxy enabled="false" >
    </defaultProxy>
  </system.net>

感谢 Ionos 的支持,我通过添加此代码设法获得代理以使其正常工作

request.Proxy = new WebProxy({proxy}, {proxy_port});

如果您有任何问题,请尝试TheGunners的解决方案,或者让您的供应商给您代理

仍然很遗憾我不得不要求它,但是嘿,它现在可以工作了