欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net获取URL和IP地址的方法汇总

程序员文章站 2024-03-02 20:49:40
httpcontext.current.request.url.tostring() 并不可靠。 如果当前url为 http://localhost/search.asp...

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>