ASP. NET get URL and IP address

Source: Internet
Author: User
Tags get ip visibility

(Transferred from:

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

If the current URL is

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

http://localhost/search.aspxuser=¼ ¼Êõ

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

1. Get through ASP.
If the URL address of the test is, the result is as follows:

2. Get through JS

<table width=100% cellpadding=0 cellspacing=0 border=0 >


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


Thisdloc = document.location; <BR>

Thisurl = document. URL; <BR>

Thishref = Document.location.href; <BR>

Thissloc = self.location.href;<br>


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


Thistloc = Top.location.href; <BR>

Thisploc = parent.document.location; <BR>

Thisthost = Top.location.hostname; <BR>

Thishost = location.hostname;<br>


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


Get IP
1, ASP. NET to get

Get the IP address of the server:
Using System.Net;

String Myip,mymac;[] AddressList = Dns.gethostbyname (Dns.gethostname ()). AddressList;
if (addresslist.length>1)
MyIP = addresslist[0]. ToString ();
Mymac = addresslist[1]. ToString ();
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
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK ">


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

<script language= "JavaScript" >
var smacaddr= "";
var sipaddr= "";
var sdnsname= "";

var service = Locator. ConnectServer ();
Service. Security_. impersonationlevel=3;
Service. Instancesofasync (foo, ' Win32_NetworkAdapterConfiguration ');


<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 for= "foo" event= "oncompleted (Hresult,perrorobject, Pasynccontext)" language= "JScript" >


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