Azure Web应用程序 - “HttpClient无法连接到远程服务器”

时间:2018-01-09 13:26:41

标签: c# .net azure azure-web-app-service

我正在尝试使用我的天蓝网络应用中的HttpClient发出请求,但收到错误:Unable to connect to the remote server

如果我打开kudu控制台,我可以使用以下方式成功发出请求:

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

当我在本地运行程序时,它可以工作。

当我在azure web应用程序中运行该程序时,它无效。

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);

更新1:

我尝试过设置:

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

但它没有效果。

1 个答案:

答案 0 :(得分:2)

您无法从Azure Web App直接调用on = premise服务。您需要Hybrid Connection Manager才能实现此目的。请看下面的帖子:

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