从另一个网络API调用https api

问题描述 投票:0回答:1

我附加下面的代码片段,在localhost中工作得非常好,但在另一台服务器上的IIS中托管时抛出Web异常/套接字。

System.Net.Sockets.SocketException(0x80004005):连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应40.113.232.243:443

除非我添加了这一行,否则它在本地也会抛出同样的错误 - httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();

但它在iis服务器中托管时会抛出socketexception。

public async Task<string> Get()
{
        try
        {
            string uri = "https://hp-reporting-*****.azurewebsites.net/********";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Timeout = 600000;

            httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // adding this line resolved error in local but still same issue persists when hosted in iis in another server

            httpWebRequest.Method = "GET";

            HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var response = streamReader.ReadToEnd();
                // this is your code here...
                System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(response, "Root");

                return node.ToString();
            }
asp.net-web-api proxy
1个回答
0
投票

好吧,看看那条线的作用:https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest.getsystemwebproxy?view=netframework-4.7.2

在本地计算机上,您在Internet Explorer中定义了一个Web代理,您可以在拨打电话时使用该代理。在部署的IIS上,您显然没有它。

因此,要么设置服务器的确切方式是设置本地计算机,要么找到另一种方法来本地解决此问题,而不使用该本地代理。当你使它工作,然后你再次部署,它将工作。

© www.soinside.com 2019 - 2024. All rights reserved.