【发布时间】:2015-04-10 08:39:37
【问题描述】:
我正在使用HttpClient 与 API 对话。如果启用,服务器将自动将http:// 请求重定向到https://。因此,为了保护用户的 API 密钥,我想创建一个到网站的测试连接,以查看在通过 API 密钥发送之前是否被重定向。
HttpClient 正确重定向,但我似乎找不到合适的方法来确定客户端是否使用 HTTPS。我知道我可以测试https:// 是否存在于response.RequestMessage.RequestUri 中,但这似乎有点不稳定
public void DetectTransportMethod()
{
using (HttpClient client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(20);
using (HttpResponseMessage response = client.GetAsync(this.HttpHostname).Result)
{
using (HttpContent content = response.Content)
{
// check if the method is HTTPS or not
}
}
}
}
【问题讨论】:
标签: c# .net https httpclient