我有外部api,可以返回一些数据。 当我尝试curl命令时,一切正常:
curl -k -d "grant_type=client_credentials" -H "Authorization: Basic token" https://url
我已经使用相同的参数从c#发出了HttpRequest,但不是成功,而是出现错误502和错误消息
代理错误(不允许使用指定的安全套接字层(SSL)端口。ForefrontTMG未配置为允许来自此端口的SSL请求。大多数Web浏览器使用端口443进行SSL请求。)
我在同一台机器上进行curl和http请求。
通过Microsoft HttpClient请求
var handler = new WebRequestHandler();
handler.ServerCertificateValidationCallback = (requestMessage, certificate, chain, policyErrors) => true;
using (var httpClient = new HttpClient(handler))
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), $"{uri}"))
{
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {token}");
request.Content = new StringContent(String.Concat("grant_type", "=", WebUtility.UrlEncode("client_credentials")));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = httpClient.SendAsync(request).Result;
}
}
通过RestSharpClient请求
var client = new RestClient($"{uri}");
client.ClearHandlers();
client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "*/*");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", $"Basic {token}");
request.AddParameter("grant_type", "client_credentials", ParameterType.GetOrPost);
IRestResponse response = client.Execute(request);
答案 0 :(得分:0)
关于您的问题,我认为这是Forefront TMG
服务器的局限性,而不是HttpClient
的问题。请参见此article,将Forefront TMG
服务器配置为允许从443旁边的其他端口进行连接。