ASP. NET get URL and IP address

Source: Internet
Author: User
Tags get ip visibility

(Transferred from: http://www.cnblogs.com/JuneZhang/archive/2010/11/26/1888863.html)

HttpContext.Current.Request.Url.ToString () is unreliable.

If the current URL is
http://localhost/search.aspx?user=http://csharp.xdowns.com&tag=%bc%bc%ca%f5

Acquired through HttpContext.Current.Request.Url.ToString () is

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


The correct method is: HttpContext.Current.Request.Url.PathAndQuery

1. Get through ASP.
If the URL address of the test is http://www.test.com/testweb/default.aspx, the result is 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. Get through 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>

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

Get the IP address of the server:
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 connection available";
}
The MYIP address is the server-side IP address.

Gets the IP address of the client, which can be used

Get the login IP address
string ip = request.servervariables["REMOTE_ADDR"]. ToString ();
2. Get through JS
<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/> hostname: <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.