yamajia
生活每一天,快乐每一天
博客园
社区
首页
新随笔
联系
管理
订阅
asp.net获取URL和IP地址
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.PathAndQuery
1
、通过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: 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
>
posted @ 2007-04-03 11:28
小K
阅读(789)
评论(2)
编辑
收藏
所属分类:
ASP.NET
发表评论
回复
引用
查看
#1楼
2007-04-11 10:50 |
laifangsong
good!
回复
引用
#2楼
2008-04-18 15:49 |
lahm [未注册用户]
这只是获取了本地IP啊(192.168.1.***)之类的!!怎么获得外网IP呢?(路由器IP)!
新用户注册
刷新评论列表
标题
姓名
主页
Email
(博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
网站首页
新闻频道
社区
小组
博问
网摘
闪存
找找看
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
相关文章:
ASP.NET实用技巧(一)
ASP.NET跨页面传值技巧总结
ASP.NET AJAX入门系列
使用Visual Studio2005入门.Net2.0系列视频教程
ASP.NET下如何浏览客户端文件夹
学校有关于ASP.NET和JSP的课程 有必要都学习吗?
建议博问中增加ASP.NET MVC分类
深入剖析ASP.NET组件设计
相关链接:
所属分类的其他文章:
于.net开发平台项目案例集锦
发布一个很COOL的图片验证码程序[含源码]
asp.net2.0学习历程 菜鸟到中级程序员的飞跃
VS.NET开发中的小技巧
好方法,让前台绑定更简单
几句话介绍MagicAjax
ASP.NET程序中常用代码汇总(转载)
深入浅出net泛型编程
关于DropDownList的怪问题
net的辅助工具列表
最新IT新闻:
金融风暴改写富豪榜排名 巴菲特资产超盖茨
红杉资本发出严重警告:黄金时代已成历史
2008年10月11日科技博客精选
搞死开心网还是搞活他?
网络书店“新”军
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
最新评论
我的标签
我参加的小组
jQuery
[开源]ASP.NET通用权限管理系统
Castle开源框架
HH.Weblog
随笔分类
(132)
7788(38)
(rss)
ASP.NET(29)
(rss)
Asp.Net 2005学习(27)
(rss)
Asp.netUpload(大文件上传) (7)
(rss)
ASP代码(3)
(rss)
C#(2)
(rss)
CSS(5)
(rss)
DotText研究(1)
(rss)
jQuery+Prototype (3)
(rss)
SQL(17)
(rss)
收藏夹
(254)
Blog收藏(237)
(rss)
架构设计(17)
(rss)
.NET
浪 子
︶ㄣ垃圾猪的垃圾窝
〖ASP.NET2.0〗学习
aierong原创技术随笔(.Net方向应用)
该兄收藏的资料很多
Dflying Chen
Dragon-China
Jeffrey Zhao
PetShop
PetShop
Terrylee
草帽
代震军BLOG
高手。乐清牙儿
高手。乐清牙儿
懒人图库
李华顺的博客
孟子E章
设计模式 Design patterns
随风.NET点滴
还不错
谭振林(牛人)
天轰穿
网络维他命
网络维他命 资源多多
厦门 x.net
小山cnblog
一个圈子
博客园
最新随笔
1. 如何用SQL语句给表增加字段? 如何分区视图?
2. 只需四天,从零开始选购笔记本电脑【转】【荐】
3. IP地址、手机归属和身份证查询接口
4. Ajax Loading图标收集
5. IE下的开发工具包
6. [转载]5行代码实现无缝滚动
7. [发布]LinScroll (jquery插件) (用图片自定义滚动条)
8. 于.net开发平台项目案例集锦
9. ASP通用分页类[强]
10. SQL SERVER日志清除方法
11. SQL Server 2000不能远程连接的问题
12. 远程连接SQL Server 2000
13. asp.net HttpHand和HttpModule的详细解释,包括Asp.Net对Http请求的处理流程。
14. 在ASP.NET中重写URL-UrlReWrite
15. 在ASP.NET 2.0中实现URL重写
最新评论
1. re: ASP通用分页类[强]
大数据量就不行了。只能用小数据量的。 (119119)
2. re: 查询同一IP地址有多少站点
不错.~~~
(缘来是你)