介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧!
1 private string GetPath() 2 { 3 string strPath = "http://" + Request.ServerVariables["HTTP_HOST"] + Request.ServerVariables["PATH_INFO"] + "?" + Request.ServerVariables["QUERY_STRING"]; 4 if(strPath.EndsWith("?")) 5 { 6 strPath = strPath.Substring(0, strPath.Length - 1); 7 } 8 return strPath; 9 }
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获取
1 <table width=100% cellpadding=0 cellspacing=0 border=0 > 2 3 <script> 4 5 thisURL = document.URL; 6 7 thisHREF = document.location.href; 8 9 thisSLoc = self.location.href; 10 11 thisDLoc = document.location; 12 13 strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>" 14 15 strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>" 16 17 strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>" 18 19 strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>" 20 21 document.write( strwrite ); 22 23 </script> 24 25 thisDLoc = document.location; <BR> 26 27 thisURL = document.URL; <BR> 28 29 thisHREF = document.location.href; <BR> 30 31 thisSLoc = self.location.href;<BR> 32 33 <script> 34 35 thisTLoc = top.location.href; 36 37 thisPLoc = parent.document.location; 38 39 thisTHost = top.location.hostname; 40 41 thisHost = location.hostname; 42 43 strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>" 44 45 strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>" 46 47 strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>" 48 49 strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>" 50 51 document.write( strwrite ); 52 53 </script> 54 55 thisTLoc = top.location.href; <BR> 56 57 thisPLoc = parent.document.location; <BR> 58 59 thisTHost = top.location.hostname; <BR> 60 61 thisHost = location.hostname;<BR> 62 63 <script> 64 65 tmpHPage = thisHREF.split( "/" ); 66 67 thisHPage = tmpHPage[ tmpHPage.length-1 ]; 68 69 tmpUPage = thisURL.split( "/" ); 70 71 thisUPage = tmpUPage[ tmpUPage.length-1 ]; 72 73 strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>" 74 75 strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>" 76 77 document.write( strwrite ); 78 79 </script><tr><td> 80 81 ================= 82 获取IP 83 1、ASP.NET中获取 84 85 获取服务器的IP地址: 86 using System.Net; 87 88 string myIP,myMac; 89 System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; 90 if ( addressList.Length>1) 91 { 92 myIP = addressList[0].ToString(); 93 myMac = addressList[1].ToString(); 94 } 95 else 96 { 97 myIP = addressList[0].ToString(); 98 myMac = "没有可用的连接"; 99 }
myIP地址就是服务器端的ip地址。
获取客户端的ip地址,可以使用
//获取登录者ip地址
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
2、通过JS获取
1 <html> 2 <head> 3 <title></title> 4 <meta http-equiv="Content-Type" content="text/html; charset=gbk"> 5 </head> 6 7 <body> 8 9 <object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object> 10 <object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object> 11 12 <form name="myForm"> 13 <br/>MAC地址:<input type="text" name="macAddress"> 14 <br/>IP地址:<input type="text" name="ipAddress"> 15 <br/>主机名:<input type="text" name="hostName"> 16 </form> 17 18 </body> 19 </html> 20 <script language="javascript"> 21 var sMacAddr=""; 22 var sIPAddr=""; 23 var sDNSName=""; 24 25 var service = locator.ConnectServer(); 26 service.Security_.ImpersonationLevel=3; 27 service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration'); 28 29 </script> 30 31 <script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript"> 32 if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){ 33 if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined") 34 sIPAddr = objObject.IPAddress(0); 35 if(objObject.MACAddress != null &&objObject.MACAddress != "undefined") 36 sMacAddr = objObject.MACAddress; 37 if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined") 38 sDNSName = objObject.DNSHostName; 39 } 40 </script> 41 42 <script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript"> 43 44 myForm.macAddress.value=sMacAddr; 45 myForm.ipAddress.value=sIPAddr; 46 myForm.hostName.value=sDNSName; 47 </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.
}