`
radio123
  • 浏览: 203425 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

asp.net、js中获取URL和IP地址方法大全

    博客分类:
  • js
 
阅读更多

转自http://yangyong2008620.blog.163.com/blog/static/859071622009927111027540/

asp.net、js中获取URL和IP地址方法大全  

2009-10-27 11:10:27|  分类: 学习资料 |  标签: |字号 订阅

  asp.net、js中获取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=¼¼Ê&otilde ;


正确的方法是: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=100% cellpadding=0 cellspacing=0 border=0 >

<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-1 ];

tmpUPage = thisURL.split( "/" );

thisUPage = tmpUPage[ tmpUPage.length-1 ];

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
1、ASP.NET中获取

获取服务器的IP地址:
using System.Net;

string myIP,myMac;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>1)
{
myIP = addressList[0].ToString();
myMac = addressList[1].ToString();
}
else
{
myIP = addressList[0].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>

分享到:
评论

相关推荐

    【ASP.NET编程知识】asp.net获取URL和IP地址的方法汇总.docx

    本文将总结 ASP.NET 中获取 URL 和 IP 地址的方法。 一、使用 HttpContext.Current.Request.Url.ToString() 获取 URL 在 ASP.NET 中,可以使用 HttpContext.Current.Request.Url.ToString() 方法来获取当前的 URL...

    Web程序设计--ASP.NET实用网站开发 课后习题答案

    * 一台 IIS Web 服务器的 IP 地址为 210.78.60.19,网站端口号为 8000,则要访问虚拟目录 xxxy 中 default.aspx 的 URL 为 http://210.78.60.19:8000/xxxy/default.aspx。 四、Visual Studio 2008 * Visual Studio...

    2.ASP.NET.2.0.高级编程(第4版) [1/7]

    内容简介回到顶部↑本书全面介绍了ASP.NET各种编程技能和2.0版中的巨大变化,并详细阐述了2.0版中的每个新特性。书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合...

    ASP.NET2.0高级编程(第4版)1/6

    本书全面介绍了ASP.NET各种编程技能和2.0版中的巨大变化,并详细阐述了2.0版中的每个新特性。书中提供了大量的实例,可帮助读者快速掌握如何在.NET平台下开发功能强大的ASP.NET应用程序。本书适合有一些基础的ASP...

    ASP.NET通用权限管理系统(FrameWork) 1.0.8源码版

    3.Url地址权限判断,需要做长度对比.如设置defautl.aspx?cmd=122 则会自动匹配 defautl.aspx?cmd=122xxxxxxx 4.修改模块分类,提示请输入权限名称,其内容不可以为空 5.插入在线人员出错,重复值InsertOnlineUser 6....

    javascript与asp.net(c#)互相调用方法

    ResolveUrl(“../PayCenter/AlipayAdd.aspx”)%&gt;’&gt;地址&lt;/a&gt; 2、C#调用客户端方法:(top.window.document:表父窗体,如果是MainFrame对象在本窗体中,则不加top.window.document) Js中的方法: 代码如下: ...

    Javascript解析URL方法详解

    可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用”&”符号隔开,每个参数的名和值用”=”符号隔开。 fragment = 信息片断 字符串,用于

    asp+sql讯时新闻管理系统源码_3.5

    1.本新闻系统永久免费,绝不过期! 2.本新闻系统采用ASP+ACCESS数据库,对一般服务器空间都支持良好。 3、框架(iframe)和JS两种调用新闻和图片...15、可进行IP地址阻止设置,防止恶意人员破坏。 16、支持RSS读取新闻。

    ASP200问.EXE

    50.如何获取用户真实的IP地址 52.如何判断网站的虚拟物理路径 53.如何解决URL含有特殊字符引发的错误 第4章 ASP组件 55.如何使用Browser Capabilities组件获取浏览器信息 56.如何使用Ad Rotator组件实现页面导航 ...

    pc-server-mobile:基于XAMPP+Ajax+nodeJS的实现pc端浏览器、服务器以及移动端浏览器三方通信的解决方案

    pc-server-mobile装逼利器:基于XAMPP+Ajax+nodeJS的实现pc端浏览器、服务器以及移动端浏览器三方通信的解决方案一步步教你实现三方通信前期工作首先,你要把js/mobile.js,js/slide.js中Ajax请求的IP地址更换一下,...

    Ayurep404换域名自动跳转

    1.设置站点的404错误页url指向到404.asp(如果不是.net站点第2步可以不管) 2.选择.net版本为2.0(也可以为1.0或4.0,须自行改web.config文件中的customErrors标签) 3.建议自己再做个index.html的js延迟10秒自动...

    JavaScript网页特效范例宝典源码

    实例110 使用toLocaleString()方法获取本地时间 176 实例111 全中文显示日期 176 实例112 在状态栏中显示日期时间 178 实例113 使用数组显示星期 179 实例114 在表格中显示时间 180 实例115 退出页面时显示停留时间 ...

    Web程序设计计算机科学经典教材.doc

    21 1.9.9 Servlet、JavaServer Pages和 JavaServer Faces概述 22 1.9.10 ASP.NET概述 22 1.9.11 Ruby概述 23 1.9.12 Rails概述 23 1.10 本章小结 24 1.11 复习题 25 1.12 练习题 26 第2章 XHTML简介 29 2.1 HTML和...

    Winson.Framework 1.0发布!

    &lt;br&gt;1、返回上一个页面的地址 2、获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在)) 3、获得当前完整Url地址 4、过滤危险字符串 5、替换html中的特殊字符 6、恢复html中...

    搜索引擎代码

    6.升级优化客户端蜘蛛的部分功能:修正入口地址设置中屏蔽的url关键词设置后无效的问题;修正定时更新,间隔更新设置后,按钮无法提交的问题;蜘蛛程序每执行一阶段采集任务后,自动释放cpu和内存,避免蜘蛛一直执行...

    正则表达式

    JavaScript的RegExp对象和String对象定义了使用正则表达式来执行强大的模式匹配和文本检索与替换函数的方法. 在JavaScript中,正则表达式是由一个RegExp对象表示的.当然,可以使用一个RegExp()构造函数来创建RegExp...

    vc++ 开发实例源码包

    ip地址扫描,发送邮箱。 ResizableLib 测试开源界面库Resizable。 RsPicture 自定义了一个图片库,然后引用测试。 SimplePlayer 简单的媒体播放源码。 Skin_Combo_Box_demo 自绘Combox控件的实例。 SkinList_...

    在b/s开发中经常用到的javaScript技术整理

    在b/s开发中经常用到的javaScript技术整理 一、验证类 1、数字验证内 1.1 整数 1.2 大于0的整数 (用于传来的ID的验证) 1.3 负整数的验证 1.4 整数不能大于iMax 1.5 整数不能小于iMin 2、时间类 ...

Global site tag (gtag.js) - Google Analytics