calling a https api from another web api

0

I am attaching the below piece of code which works perfectly fine in localhost but throws web exception/socket when hosted in IIS on another server.

System.Net.Sockets.SocketException (0x80004005): 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 40.113.232.243:443

It was throwing the same error in local too, unless I added this line- httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();

yet it throws socketexception when hosted in iis server.

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
asked on Stack Overflow Feb 27, 2019 by sreejani das • edited Feb 27, 2019 by iElden

1 Answer

0

well, look at what that line does : https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest.getsystemwebproxy?view=netframework-4.7.2

On your local machine, you have a web proxy defined in Internet Explorer which you use when making the call. On the deployed IIS you clearly don't have it.

So, either you setup the server exactly how you setup your local machine or find another way to solve this issue locally, without using that local proxy. When you get it working, then you deploy again and it will work.

answered on Stack Overflow Feb 28, 2019 by Andrei Dragotoniu

User contributions licensed under CC BY-SA 3.0