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
阅读(769)
评论(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 AJAX入门系列
使用Visual Studio2005入门.Net2.0系列视频教程
ASP.NET 大文件上传组件[无组件上传](AspnetUpload 2.3 release)[免费版序列号放送中...]
【翻译】Scott Mitchell的ASP.NET2.0数据指南中文版索引
.NET设计模式系列文章
关于IE问题,请教和求救
相关链接:
所属分类的其他文章:
于.net开发平台项目案例集锦
发布一个很COOL的图片验证码程序[含源码]
asp.net2.0学习历程 菜鸟到中级程序员的飞跃
VS.NET开发中的小技巧
好方法,让前台绑定更简单
几句话介绍MagicAjax
ASP.NET程序中常用代码汇总(转载)
深入浅出net泛型编程
关于DropDownList的怪问题
net的辅助工具列表
最新IT新闻:
Google推出Android Market挑战App Store
美国年轻人最喜欢的15大网站
2008年8月30日IT博客精选
《极品飞车12》最新真人照片、游戏截图
IBM正在开发超强性能4TB固态硬盘阵列
博客园新闻频道
博客园首页
社区
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
最新评论
我的标签
我参加的小组
jQuery
[开源]ASP.NET通用权限管理系统
Castle开源框架
HH.Weblog
随笔分类
(131)
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(16)
(rss)
收藏夹
(252)
Blog收藏(235)
(rss)
架构设计(17)
(rss)
.NET
浪 子
︶ㄣ垃圾猪的垃圾窝
〖ASP.NET2.0〗学习
aierong原创技术随笔(.Net方向应用)
该兄收藏的资料很多
Dflying Chen
Dragon-China
Jeffrey Zhao
PetShop
PetShop
草帽
高手。乐清牙儿
高手。乐清牙儿
懒人图库
李华顺的博客
孟子E章
设计模式 Design patterns
随风.NET点滴
还不错
谭振林(牛人)
天轰穿
网络维他命
网络维他命 资源多多
厦门 x.net
小山cnblog
一个圈子
博客园
最新随笔
1. IP地址、手机归属和身份证查询接口
2. Ajax Loading图标收集
3. IE下的开发工具包
4. [转载]5行代码实现无缝滚动
5. [发布]LinScroll (jquery插件) (用图片自定义滚动条)
6. 于.net开发平台项目案例集锦
7. ASP通用分页类[强]
8. SQL SERVER日志清除方法
9. SQL Server 2000不能远程连接的问题
10. 远程连接SQL Server 2000
11. asp.net HttpHand和HttpModule的详细解释,包括Asp.Net对Http请求的处理流程。
12. 在ASP.NET中重写URL-UrlReWrite
13. 在ASP.NET 2.0中实现URL重写
14. 全球十位顶尖photoshop设计大师
15. Windows下Subversion配置管理员指南
最新评论
1. re: 让FckEditor具有自动保存功能 或是读取 FckEditor 的值
顶下楼主上面的取值做法貌似不对,至少我没取到值后来我按照下面的做法取到了<code>var oEditor = FCKeditorAPI.GetInstance('InstanceName... (cnc224)
2. re: 在ASP.NET中重写URL-UrlReWrite
按照这个配置不成功,请问什么问题<?xml version="1.0" encoding="UTF-8"?><configuration>... (jameswilliam)
3. re: 大型企业门户网站设计开发一般性原则和建议
收!!! (BeatOfAngle)