asp.net to get URL and IP Address method Summary _ Practical Tips

Source: Internet
Author: User
Tags get ip httpcontext visibility

HttpContext.Current.Request.Url.ToString () is not reliable.

If the current URL is
Http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%BC%BC%CA%F5

The HttpContext.Current.Request.Url.ToString () gets the

Http://localhost/search.aspxuser=http://csharp.xdowns.com&tag=¼¼êõ


The correct approach is to:

Copy Code code as follows:

HttpContext.Current.Request.Url.PathAndQuery

1, through the asp.net to obtain
If the URL address for the test is http://www.test.com/testweb/default.aspx, the result is as follows:
Copy Code code as follows:

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, through JS access
Copy Code code as follows:

<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>


=================
Get IP
1, ASP. NET to get

To obtain the server's IP address:

Copy Code code as follows:

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 = "no connections available";
}


The MYIP address is the server-side IP address.

Gets the IP address of the client, which you can use

Copy Code code as follows:

Get the IP address of the logged-on person
string ip = request.servervariables["REMOTE_ADDR"]. ToString ();

2, through JS access
Copy Code code as follows:

<title></title>
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">

<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 Address: <input type= "text" name= "macAddress" >
<br/>ip Address: <input type= "text" Name= "IPAddress" >
<br/> Host Name: <input type= "text" name= "HostName" >
</form>

</body>
<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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.