1

I'm trying to make a request using HttpClient in my azure web app but I get the error: Unable to connect to the remote server

If I open the kudu console I can successfully make the request using:

Invoke-RestMethod -Uri https://mydomain/myendpoint

When I run the program locally it works.

When I run the program inside an azure web app it does not work.

HttpClient _client;        

public MyHttpClient() {
  _client = new HttpClient();
  _client.BaseAddress = new System.Uri(ConfigurationManager.AppSettings["MyUri"]);          
  _client.DefaultRequestHeaders.Accept.Clear();            
  _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));        }

//...

  var path = "/myendpoint";
  var data = new Dictionary<string, string>();        
  data.Add("param1", value);        
  var response = await _client.PostAsJsonAsync(path, data);

Update 1:

I tried setting:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | 
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;

but it had no effect.

Eamonn McEvoy
  • 8,876
  • 14
  • 53
  • 83
  • Take a look here: https://stackoverflow.com/questions/22251689/make-https-call-using-httpclient – LocEngineer Jan 09 '18 at 13:32
  • I think the issue is mostly because of TLS protocol. You may need to explicitly set this before making "PostAsJsonAsync" call. Check the link provided by @LocEngineer – Nirman Jan 09 '18 at 13:50

1 Answers1

2

You can not make direct call to your on=premise service from Azure Web App. You need Hybrid Connection Manager for this purpose. Look at below post:

Azure web app service to call onpremise WEB API using HttpClient using hybrid connection manager

arpan desai
  • 889
  • 2
  • 13
  • 23