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 / { ...
随机推荐
- ZOJ 3872--解题报告
题目相关: 3872相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5520 Edward拥有一组数列. 其定义了Be ...
- linux 安装redis
1:首先命令行下载安装包 wget http://download.redis.io/releases/redis-2.8.13.tar.gz 2:进行解压 tar xzf redis-2.8.13. ...
- SQL注入测试平台 SQLol -5.DELETE注入测试
访问首页的delete模块,http://127.0.0.1/sql/delete.php,开始对delete模块进行测试. delete语法: DELETE FROM [users] WHERE [ ...
- MATLAB符号运算 分类: 图像处理 2015-07-31 22:53 3人阅读 评论(0) 收藏
1.符号运算 使用MATLAB可以进行多项式乘除运算,也可以进行因式分解. 例1. 多项式乘除运算(x+3)3 >> syms x; >> expand((x+3)^3) an ...
- SQL Server用户自定义函数
用户自定义函数不能用于执行一系列改变数据库状态的操作,但它可以像系统 函数一样在查询或存储过程等的程序段中使用,也可以像存储过程一样通过EXECUTE 命令来执行.在 SQL Server 中根据函数 ...
- nfs文件系统启动参数配置
1. tiny6410(增强版)bootargs(nfs文件挂载)启动参数(周学伟) noinitrd console=ttySAC0,115200 lcd=S70 init=/init root=/ ...
- 【BZOJ1011】【HNOI2008】遥远的行星
奇奇怪怪突然出戏的奇葩题 原题: 直线上N颗行星,X=i处有行星i,行星J受到行星I的作用力,当且仅当i<=AJ.此时J受到作用力的大小为 Fi->j=Mi*Mj/(j-i) 其中A为很小 ...
- 【BZOJ1005】【HNOI2008】明明的烦恼
又是看黄学长的代码写的,估计我的整个BZOJ平推计划都要看黄学长的代码写 原题: 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连 ...
- 【转】iOS10项目打包上传被拒关于隐私权限问题
原文网址:http://blog.csdn.net/yidu_blog/article/details/53064987 今天项目打包提交.收到了苹果的邮件.主要内容: This app attemp ...
- 用max-width来防止图片溢出---不兼容ie6-----将max-width设置成父元素的宽度,当图片过大时会换行,以及限制图片的宽度。但是如果使用table,仍然可能超出限制。
img{ max-width:100%; height:auto; } 如题.