如何判断 HttpClient 是否通过 HTTPS 连接
How can I tell if HttpClient is connecting via HTTPS
我正在使用 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
}
}
}
}
HttpClient
(MSDN) and HttpResponseMessage
(MSDN) do not contain any methods that can be used to determine whether a request has been made over https. Even though checking the URI of the HttpResponseMessage
does indeed sound flaky I'm afraid it's the easiest and most readable option. Implementing this as a extension method for HttpResponseMessage
probably is the most readable. To ensure that the HttpClient
you are using can be redirected make sure that the WebRequestHandler
(MSDN) passed into the HttpClient
has the AllowAutoRedirect
(MSDN) 属性 的文档设置为 true。
查看以下扩展方法:
static class Extensions
{
public static bool IsSecure(this HttpResponseMessage message)
{
rreturn message.RequestMessage.RequestUri.Scheme == "https";
}
}
下面的控制台应用程序演示了它的工作原理。但是,为了使其工作,HTTP 服务器必须将连接升级到 https(就像 Facebook 所做的那样)并且 WebRequestHandler
的 AllowAutoRedirect
属性 必须设置为 true
.
static void Main(string[] args)
{
using (var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true}))
{
client.Timeout = TimeSpan.FromSeconds(20);
using (var response = client.GetAsync("http://www.geenstijl.nl").Result)
{
Console.WriteLine(response.IsSecure());
}
using (var response = client.GetAsync("http://www.facebook.com").Result)
{
Console.WriteLine(response.IsSecure());
}
}
Console.ReadLine();
}
我正在使用 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
}
}
}
}
HttpClient
(MSDN) and HttpResponseMessage
(MSDN) do not contain any methods that can be used to determine whether a request has been made over https. Even though checking the URI of the HttpResponseMessage
does indeed sound flaky I'm afraid it's the easiest and most readable option. Implementing this as a extension method for HttpResponseMessage
probably is the most readable. To ensure that the HttpClient
you are using can be redirected make sure that the WebRequestHandler
(MSDN) passed into the HttpClient
has the AllowAutoRedirect
(MSDN) 属性 的文档设置为 true。
查看以下扩展方法:
static class Extensions
{
public static bool IsSecure(this HttpResponseMessage message)
{
rreturn message.RequestMessage.RequestUri.Scheme == "https";
}
}
下面的控制台应用程序演示了它的工作原理。但是,为了使其工作,HTTP 服务器必须将连接升级到 https(就像 Facebook 所做的那样)并且 WebRequestHandler
的 AllowAutoRedirect
属性 必须设置为 true
.
static void Main(string[] args)
{
using (var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true}))
{
client.Timeout = TimeSpan.FromSeconds(20);
using (var response = client.GetAsync("http://www.geenstijl.nl").Result)
{
Console.WriteLine(response.IsSecure());
}
using (var response = client.GetAsync("http://www.facebook.com").Result)
{
Console.WriteLine(response.IsSecure());
}
}
Console.ReadLine();
}