实现JS ASP NET PHP JAVA语言获取判断http与htpps协议头方法

作者: unvs 分类: ASP.NET, PHP 发布时间: 2016-12-16 10:33 ė14,409 views 6没有评论

本文博主收集了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,原理差不多,就是各语言写法不同,这里整理出来,分享给大家。

本博文章基本上属于原创或收集整理,都是心血结晶。
欢迎转载分享,转载请注明出处,谢谢!
本文地址:实现JS ASP NET PHP JAVA语言获取判断http与htpps协议头方法

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Ɣ回顶部