asp.net和脚本获取当前的URL、IP地址
介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧!
private string GetPath()
{
string strPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.ServerVariables["PATH_INFO"] + "?" + Request.ServerVariables["QUERY_STRING"];
if(strPath.EndsWith("?"))
{
strPath = strPath.Substring(, strPath.Length - );
}
return strPath;
}
asp.net获取URL和IP地址
HttpContext.Current.Request.Url.ToString() 并不可靠。
如果当前URL为
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5
通过HttpContext.Current.Request.Url.ToString()获取到的却是
http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼¼Êõ
正确的方法是:HttpContext.Current.Request.Url.PathAndQuery1、通过ASP.NET获取
如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath: /testweb
Request.CurrentExecutionFilePath: /testweb/default.aspx
Request.FilePath: /testweb/default.aspx
Request.Path: /testweb/default.aspx
Request.PhysicalApplicationPath: E:/WWW/testwebRequest.PhysicalPath: E:/WWW/testweb/default.aspx
Request.RawUrl: /testweb/default.aspx
Request.Url.AbsolutePath: /testweb/default.aspx
Request.Url.AbsoluteUrl: http://www.test.com/testweb/default.aspx
Request.Url.Host: http://www.test.com/
Request.Url.LocalPath: /testweb/default.aspx
2、通过JS获取
<table width=% cellpadding= cellspacing= border= > <script> thisURL = document.URL; thisHREF = document.location.href; thisSLoc = self.location.href; thisDLoc = document.location; strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>" strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>" strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>" strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>" document.write( strwrite ); </script> thisDLoc = document.location; <BR> thisURL = document.URL; <BR> thisHREF = document.location.href; <BR> thisSLoc = self.location.href;<BR> <script> thisTLoc = top.location.href; thisPLoc = parent.document.location; thisTHost = top.location.hostname; thisHost = location.hostname; strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>" strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>" strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>" strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>" document.write( strwrite ); </script> thisTLoc = top.location.href; <BR> thisPLoc = parent.document.location; <BR> thisTHost = top.location.hostname; <BR> thisHost = location.hostname;<BR> <script> tmpHPage = thisHREF.split( "/" ); thisHPage = tmpHPage[ tmpHPage.length- ]; tmpUPage = thisURL.split( "/" ); thisUPage = tmpUPage[ tmpUPage.length- ]; strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>" strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>" document.write( strwrite ); </script><tr><td> =================
获取IP
、ASP.NET中获取 获取服务器的IP地址:
using System.Net; string myIP,myMac;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>)
{
myIP = addressList[].ToString();
myMac = addressList[].ToString();
}
else
{
myIP = addressList[].ToString();
myMac = "没有可用的连接";
}
myIP地址就是服务器端的ip地址。
获取客户端的ip地址,可以使用
//获取登录者ip地址
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
2、通过JS获取
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head> <body> <object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object> <form name="myForm">
<br/>MAC地址:<input type="text" name="macAddress">
<br/>IP地址:<input type="text" name="ipAddress">
<br/>主机名:<input type="text" name="hostName">
</form> </body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName=""; var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); </script> <script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</script> <script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript"> myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;
</script>
整理:
Request.ServerVariables("REMOTE_ADDR") 来取得客户端的IP地址,但如果客户端是使用代理服务器来访问,那取到的就是代理服务器的IP地址,而不是真正的客户端IP地址。
要想透过代理服务器取得客户端的真实IP地址,就要使用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取。
不过要注意的事,并不是每个代理服务器都能用 Request.ServerVariables("HTTP_X_FORWARDED_FOR") 来读取客户端的真实 IP,有些用此方法读取到的仍然是代理服务器的IP。
还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 Request.ServerVariables ("HTTP_X_FORWARDED_FOR") 取到的值将是空的。因此,如果要在程序中使用此方法,可以这样处理:
......
userip = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If userip = "" Then userip = Request.ServerVariables("REMOTE_ADDR")
......//方法一
HttpContext.Current.Request.UserHostAddress;//方法二
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//方法三
string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();//方法四(无视代理)
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];客户端:
//方法五
var ip = '<!--#echo var="REMOTE_ADDR"-->';
alert("Your IP address is "+ip);//方法六(无视代理)
function GetLocalIPAddress()
{
var obj = null;
var rslt = "";
try
{
obj = new ActiveXObject("rcbdyctl.Setting");
rslt = obj.GetIPAddress;
obj = null;
}
catch(e)
{
//
}
return rslt;
}
来自印度的MCT Maulik Patel提供了一种服务端的解决方案,很好:if(Context.Request.ServerVariables["HTTP_VIA"]!=null) // 服务器, using proxy
{
得到真实的客户端地址
ip=Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP.
}
else//如果没有使用代理服务器或者得不到客户端的ip not using proxy or can't get the Client IP
{
//得到服务端的地址
ip=Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
}
asp.net和脚本获取当前的URL、IP地址的更多相关文章
- python3脚本获取本机公网ip
python脚本获取本机公网ip 1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.代码实现 import requests import re r ...
- nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容
1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...
- python之获取微信服务器的ip地址
# -*- coding: cp936 -*- #PYTHON 27 #xiaodeng #获取微信服务器的ip地址 import urllib url='https://api.weixin.qq. ...
- 通过HttpservletRequest对象获取客户端的真实IP地址
这篇文章主要介绍了Java中使用HttpRequest获取用户真实IP地址,使用本文方法可以避免Apache.Squid.nginx等反向代理软件导致的非真实IP地址,需要的朋友可以参考下 在JSP里 ...
- 【Win 10 应用开发】获取本机的IP地址
按照老规矩,也是朋友的建议,老周今天在吹牛之前,先讲一个小故事. 有朋友问我,老周,你现在还发短信吗,你每个月用多少电话费?唉,实话说,现在真的发短信不多了,套餐送的130条短信,每月都发不了一条.至 ...
- 获取本机的IP地址(局域网)与主机名称
编写内容保存为bat @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址(局域网)=========*/ e ...
- PHP获取APP客户端的IP地址的方法
分析php获取客户端ip 用php能获取客户端ip,这个大家都知道,代码如下: /** * 获取客户端ip * @param number $type * @return string */ func ...
- 获取当前设备的IP地址
头文件: #import <ifaddrs.h> #import <arpa/inet.h> #import <net/if.h> 宏定义: #define IOS ...
- 服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址
首先,在nginx配置中添加如下配置 server { listen ; server_name www.wenki.info; #要访问的域名 charset utf8; location / { ...
随机推荐
- poj 3320 技巧/尺取法 map标记
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- 一个textview实现文字在图片上面的效果
类似于这样的,其实很简单,可是以前用的是imageview+textview.布局实现多写了好多代码.又不能在图片加文字,显得没技术含量. xml代码如下: <TextView android: ...
- jsp不能引用js,cs等解决办法
最近项目中使用到Spring3,在感叹Spring3注解配置清爽的同时竟然出现了这个不和谐的事情,实在无法忍受 问题:部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52 ...
- 安装sklearn时出现 "ImportError: DLL load failed" 的解决方法
如果sklearn是从 http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn 中下的whl包装的 必须装他家的numpy+MKL库.如果你装的 ...
- nginx绑定多个域名
nginx绑定多个域名涉及到的技术为url rewrite,可以先了解下知识背景再过来学习. 这里以域名:www.sample.com为例 1.在/usr/local/nginx/conf文件夹中创建 ...
- jQuery使用伪递归重复执行动画
使用setInterval()来重复执行动画,会因为动画执行过程的时候,setInterval()的时间依然是在走的,所以会导致动画的调用时间不理想,因此只能使用递归来重复执行动画. // 首页LOG ...
- Qt GUI@学习日志
day 1: Qt中类: 理解一个类最好还是从其类代码实现上看. 由此图可看出需要好好研究那几个重要的类:Qt/QEvent/QObject/QWidget/. QApplication: (比较复杂 ...
- java 四种内部类和内部接口
/** * 常规内部类:常规内部类没有用static修饰且定义在在外部类类体中. * 1.常规内部类中的方法可以直接使用外部类的实例变量和实例方法. * 2.在常规内部类中可以直接用内部类创建对象 * ...
- docker加速器
https://cr.console.aliyun.com/#/docker/booster 阿里云开发者账号注册后,获得一专属加速器地址. 转发自: http://www.imike.me/2016 ...
- unity, GL.TexCoord or GL.Color must put before GL.Vertex!!!
GL.Begin(GL.QUADS); //in unity, should use left hand rule //RU GL.TexCo ...