从另一个Web API调用https API

时间:2019-02-27 11:08:04

标签: asp.net-web-api proxy

我附加了以下代码,该代码在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();
            }

1 个答案:

答案 0 :(得分:0)

好吧,看看那行是做什么的:https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest.getsystemwebproxy?view=netframework-4.7.2

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

因此,您可以完全按照设置本地计算机的方式来设置服务器,或者找到另一种无需使用该本地代理即可在本地解决此问题的方法。当它开始工作时,您可以再次进行部署,它将开始工作。