title: Azure web app service to call onpremise WEB API using HttpClient using hybrid connection manager
body: <p>We have onpremise web service (asp.net core mvc) deployed on local network machine. We are trying to call these WEB API using App Service deployed on Azure. But it is giving time out error or <strong>Task was cancelled</strong> error in case when we try to connect it using "HTTP" Protocol. In case of "HTTPS" it is giving "<strong>security error occurred error</strong>".</p>
  
  <p>We have created Hybrid connection on Azure App Service to connect to onpremise web api service which shows online for both 80 and 443 port. We have setup Hybrid Connection Manager on local network machine too.</p>
  
  <p>Below is the code snippet for calling code which is deployed on Azure App Service (e.g. <a href="https://xyz.azurewebsite.com" rel="nofollow noreferrer">https://xyz.azurewebsite.com</a>) </p>
  
  <pre><code> try
   {
      var httpClient = new HttpClient();
      httpClient.Timeout = TimeSpan.FromMinutes(60);
      httpClient.BaseAddress = new Uri("https://onpremise");
      httpClient.DefaultRequestHeaders.Accept.Clear();
      httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
      ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
      ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) =&gt; true;
      //Simple Get Request to test on-premise service
      var response = await httpClient.GetStringAsync("api/OnPremiseData/GetData");
      ViewBag.ResponseText = response;
   }
  </code></pre>
  
  <p>Above code works fine in case I debug application from localhost. So there is no issue with code I assume.</p>
  
  <p>Below is web api code snippet:</p>
  
  <pre><code>[Route("api/[controller]/[action]")]
  public class OnPremiseDataController : Controller
  {        
    [HttpGet]
    public string GetData()
    {
      return "Success";
    }
  }
  </code></pre>
  
  <p>and below is startup.cs file</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
    services.AddCors();
    services.AddMvc();            
  }
  // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
    app.UseCors(options =&gt; options.WithOrigins("https://xyz.azurewebsite.com", "https://localhost:44310").AllowAnyMethod().AllowAnyHeader());
    if (env.IsDevelopment())
    {
      app.UseDeveloperExceptionPage();
    }            
    app.UseMvc();                
  }
  </code></pre>