我正在使用此代码重定向
If Not Request.IsSecureConnection Then
Dim url As String = Request.Url.ToString().Replace("http:", "https:")
Response.Redirect(url)
Exit Sub
End If
现在我只想在我的网站上有 ssl 时才重定向到 HTTPS。那么如何检查 ssl 可用性呢?
使用在https://stackoverflow.com/a/5378470/2012977创建的函数可以帮助检查 url 的可用性。请参见下面的示例。
public bool UrlExists(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 15000;
request.Method = "HEAD";
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (WebException)
{
return false;
}
}