实现JS ASP NET PHP JAVA语言获取判断http与htpps协议头方法
本文博主收集了JS ASP NET PHP JAVA各种语言实现获取url是 http 还是 https 协议的方法代码,直接进入正题。
Javascript代码方法:
var ishttps = 'https:' == document.location.protocol ? true: false;
if(ishttps){
alert("这是个https请求");
}else{
alert(“这是个http请求”);
}
ASP代码方法:
<%
Response.Buffer = True
If (Request.ServerVariables("HTTPS") = "off") Then
*****
End if
%>
举例说明:
让一个ASP页面以https开始,请在该ASP页面顶部添加如下代码,
<%
Response.Buffer = True
If (Request.ServerVariables("HTTPS") = "off") Then
Dim xredir__, xqstr__
xredir__ = "https://" & Request.ServerVariables("SERVER_NAME") & _
Request.ServerVariables("SCRIPT_NAME")
xqstr__ = Request.ServerVariables("QUERY_STRING")
if xqstr__ <> "" Then xredir__ = xredir__ & "?" & xqstr__
Response.redirect xredir__
End if
%>
PHP代码方法:
if ($_SERVER['HTTPS'] != "on") {
echo "这不是个https地址";
}else{
echo "这是个https地址";
}
ASP.NET代码方法:
//string url = Page.Request.ServerVariables["HTTP_HOST"];
string url = HttpContext.Current.Request.ServerVariables["HTTPS"];//值为"off"或"on"
JAVA代码方法:
1、截取URL地址
String URL = request.getRequestURL().toString();
if(!URL.startsWith("https:"))
{
System.out.println("HTTPS");
}
2、获取https协议
if("http".equals(request.getScheme()))
System.out.println("HTTP");
if("https".equals(request.getScheme()))
System.out.println("HTTPS");
3、判断协议
if("https:" == document.location.protocol)
alert("HTTPS");
总结:获取url地址协议是否为https,原理差不多,就是各语言写法不同,这里整理出来,分享给大家。