Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

Azure web app - "HttpClient Unable to connect to the remote server"

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.

Upvotes: 1

Views: 2535

Answers (1)

arpan desai
arpan desai

Reputation: 909

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

Upvotes: 2

Related Questions