简体   繁体   中英

C# call an API through proxy using HttpWebRequest

I want to make an API call (outside my organization domain) in my existing application, the network team has told me to set up a proxy config - only then it'd have the ability to go to the internet. Dev has the ability to go to the internet without referencing the proxy setup.

In my dev environment, it is working fine since the ports are open, how to configure a proxy for UAT environment so that it can hit the new target on the internet? proxy IP is given by the network team for Non-prod: 12.XXX.XXX.0 Port 80

and also how to check:

  • if the above proxy is alive?
  • once it is configured how to check if the request is going through the proxy because I just have dev server access where all ports are open, how would I check if it is configured correctly so that once deployed on UAT it works fine?

The HttpWebRequest has a proxy property:

var myWebRequest = (HttpWebRequest)WebRequest.Create("url");
myWebRequest.Proxy = new WebProxy("host", 80);

try 
{
    var response = myWebRequest.GetResponse();
    //...
} 
catch (WebException e)
{
    // handling issues
}

If a request fails, an exception will be thrown that you can handle in catch block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM