介绍一下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地址的更多相关文章

  1. python3脚本获取本机公网ip

    python脚本获取本机公网ip   1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.代码实现 import requests import re r ...

  2. nodejs之获取客户端真实的ip地址+动态页面中引用静态路径下的文件及图片等内容

    1.nodejs获取客户端真实的IP地址: 在一般的管理网站中,尝尝会需要将用户的一些操作记录下来,并记住是哪个用户进行操作的,这时需要用户的ip地址,但是往往当这些应用部署在服务器上后,都使用了ng ...

  3. python之获取微信服务器的ip地址

    # -*- coding: cp936 -*- #PYTHON 27 #xiaodeng #获取微信服务器的ip地址 import urllib url='https://api.weixin.qq. ...

  4. 通过HttpservletRequest对象获取客户端的真实IP地址

    这篇文章主要介绍了Java中使用HttpRequest获取用户真实IP地址,使用本文方法可以避免Apache.Squid.nginx等反向代理软件导致的非真实IP地址,需要的朋友可以参考下 在JSP里 ...

  5. 【Win 10 应用开发】获取本机的IP地址

    按照老规矩,也是朋友的建议,老周今天在吹牛之前,先讲一个小故事. 有朋友问我,老周,你现在还发短信吗,你每个月用多少电话费?唉,实话说,现在真的发短信不多了,套餐送的130条短信,每月都发不了一条.至 ...

  6. 获取本机的IP地址(局域网)与主机名称

    编写内容保存为bat @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址(局域网)=========*/ e ...

  7. PHP获取APP客户端的IP地址的方法

    分析php获取客户端ip 用php能获取客户端ip,这个大家都知道,代码如下: /** * 获取客户端ip * @param number $type * @return string */ func ...

  8. 获取当前设备的IP地址

    头文件: #import <ifaddrs.h> #import <arpa/inet.h> #import <net/if.h> 宏定义: #define IOS ...

  9. 服务器使用nginx做代理,通过HttpServletRequest获取请求用户真实IP地址

    首先,在nginx配置中添加如下配置 server { listen ; server_name www.wenki.info; #要访问的域名 charset utf8; location / { ...

随机推荐

  1. 解读vmstat中的ACTIVE/INACTIVE MEMORY

    vmstat 命令能够报告关于内核线程.虚拟内存.磁盘.陷阱和 CPU 活动的统计信息,那么我们又该如何理解其工作原理呢? vmstat -a 命令能看到active memory 和 inactiv ...

  2. Java动物声音模拟器

    abstract class Animal{ abstract void cry(); abstract String getAnimalName(); } class Simulator{ void ...

  3. URAL 1137 Bus Routes(欧拉回路路径)

    1137. Bus Routes Time limit: 1.0 secondMemory limit: 64 MB Several bus routes were in the city of Fi ...

  4. JSBinding + SharpKit / 实战:转换 Survival Shooter

    从 asset store 下载 Survival Shooter (商店里有2个版本,一种是给Unity5用的,一个是给Unity4.6用的,我们这个实验用的是后者,版本是2.2.如果) 1 删除多 ...

  5. 开源app之MyHearts

    前言 这个月,说实话,有忙有闲,经历了一次病痛的洗礼,才认识到了只有好好的生活,认真的对待自己的身体,才能更好的去工作,没有了身体的支撑,什么工作都只能是纸老虎,不攻自破.在这里也祝愿大家,在生活中好 ...

  6. 在apache连接多php的时候遇到了问题,怎么切换多个php版本?

    PHP 在apache连接多php的时候遇到了问题,怎么切换多个php版本? 我的机器里面有一个apache2.2.22,但是有两个php,5.3.10和5.4.3,5.3.10是mac os x带的 ...

  7. SVG中的'defs' and 'use'-可复用的图元定义

    在下一个示例中,我使用了defs中的元素之前,定义了如何去展现图元. <?xml version="1.0" standalone="no"?> & ...

  8. call和apply的差别

    Call(thisArg,arg1,arg2,-..) apply(thisArg,[arg1,arg2-.])

  9. Android Afinal框架(二)

    框架地址:https://github.com/yangfuhai/afinal 对应的源码: net.tsz.afinal.annotation.view.* FinalActivity Final ...

  10. Jquery遍历元素

    页面中所有的checkBox遍历: <script type="text/javascript" src="/jquery/jquery.js">& ...