﻿{"id":644,"date":"2010-06-20T19:38:00","date_gmt":"2010-06-20T11:38:00","guid":{"rendered":"http:\/\/blog.jixin.ntxz.net\/?p=644"},"modified":"2010-07-30T15:38:50","modified_gmt":"2010-07-30T07:38:50","slug":"asp-net%e3%80%81js%e4%b8%ad%e8%8e%b7%e5%8f%96url%e5%92%8cip%e5%9c%b0%e5%9d%80%e6%96%b9%e6%b3%95%e5%a4%a7%e5%85%a8","status":"publish","type":"post","link":"http:\/\/www.ntxz.net\/?p=644","title":{"rendered":"asp.net\u3001js\u4e2d\u83b7\u53d6URL\u548cIP\u5730\u5740\u65b9\u6cd5\u5927\u5168"},"content":{"rendered":"<p>&nbsp;asp.net\u3001js\u4e2d\u83b7\u53d6URL\u548cIP\u5730\u5740\u65b9\u6cd5\u5927\u5168 \u6536\u85cf <br \/>HttpContext.Current.Request.Url.ToString() \u5e76\u4e0d\u53ef\u9760\u3002 <\/p>\n<p>\u5982\u679c\u5f53\u524dURL\u4e3a <br \/><a href=\"http:\/\/localhost\/search.aspx?user=http:\/\/csharp.xdowns.com&amp;tag=%BC%BC%CA%F5\"><font color=#e83900>http:\/\/localhost\/search.aspx?user=http:\/\/csharp.xdowns.com&amp;tag=%BC%BC%CA%F5<\/font><\/a> <\/p>\n<p>\u901a\u8fc7HttpContext.Current.Request.Url.ToString()\u83b7\u53d6\u5230\u7684\u5374\u662f <\/p>\n<p><a href=\"http:\/\/localhost\/search.aspxuser=http:\/\/csharp.xdowns.com&amp;tag=&amp;frac14;&amp;frac14;&amp;Ecirc;&amp;otilde\"><font color=#e83900>http:\/\/localhost\/search.aspxuser=http:\/\/csharp.xdowns.com&amp;tag=&amp;frac14;&amp;frac14;&amp;Ecirc;&amp;otilde<\/font><\/a> ; <\/p>\n<p>\u6b63\u786e\u7684\u65b9\u6cd5\u662f\uff1aHttpContext.Current.Request.Url.PathAndQuery<\/p>\n<p>1\u3001\u901a\u8fc7ASP.NET\u83b7\u53d6<br \/>\u5982\u679c\u6d4b\u8bd5\u7684url\u5730\u5740\u662f<a href=\"http:\/\/www.test.com\/testweb\/default.aspx\"><font color=#e83900>http:\/\/www.test.com\/testweb\/default.aspx<\/font><\/a> , \u7ed3\u679c\u5982\u4e0b\uff1a<br \/>Request.ApplicationPath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb<br \/>Request.CurrentExecutionFilePath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx<br \/>Request.FilePath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx<br \/>Request.Path:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx<br \/>Request.PhysicalApplicationPath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; E:\\WWW\\testwebRequest.PhysicalPath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; E:\\WWW\\testweb\\default.aspx<br \/>Request.RawUrl:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx<br \/>Request.Url.AbsolutePath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx<br \/>Request.Url.AbsoluteUrl:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/www.test.com\/testweb\/default.aspx\"><font color=#e83900>http:\/\/www.test.com\/testweb\/default.aspx<\/font><\/a> <br \/>Request.Url.Host:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/www.test.com\/\"><font color=#e83900>http:\/\/www.test.com\/<\/font><\/a> <br \/>Request.Url.LocalPath:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/testweb\/default.aspx <\/p>\n<p>2\u3001\u901a\u8fc7JS\u83b7\u53d6<\/p>\n<p>&lt;table width=100% cellpadding=0 cellspacing=0 border=0 &gt;<\/p>\n<p>&lt;script&gt; <\/p>\n<p>thisURL = document.URL; <\/p>\n<p>thisHREF = document.location.href; <\/p>\n<p>thisSLoc = self.location.href; <\/p>\n<p>thisDLoc = document.location; <\/p>\n<p>strwrite = &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisURL: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisURL + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisHREF: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisHREF + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisSLoc: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisSLoc + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisDLoc: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisDLoc + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>document.write( strwrite ); <\/p>\n<p>&lt;\/script&gt; <\/p>\n<p>thisDLoc = document.location; &lt;BR&gt;<\/p>\n<p>thisURL = document.URL; &lt;BR&gt;<\/p>\n<p>thisHREF = document.location.href; &lt;BR&gt;<\/p>\n<p>thisSLoc = self.location.href;&lt;BR&gt;<\/p>\n<p>&lt;script&gt; <\/p>\n<p>thisTLoc = top.location.href; <\/p>\n<p>thisPLoc = parent.document.location; <\/p>\n<p>thisTHost = top.location.hostname; <\/p>\n<p>thisHost = location.hostname; <\/p>\n<p>strwrite = &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisTLoc: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisTLoc + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisPLoc: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisPLoc + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisTHost: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisTHost + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisHost: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisHost + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>document.write( strwrite ); <\/p>\n<p>&lt;\/script&gt; <\/p>\n<p>thisTLoc = top.location.href; &lt;BR&gt;<\/p>\n<p>thisPLoc = parent.document.location; &lt;BR&gt;<\/p>\n<p>thisTHost = top.location.hostname; &lt;BR&gt;<\/p>\n<p>thisHost = location.hostname;&lt;BR&gt;<\/p>\n<p>&lt;script&gt; <\/p>\n<p>tmpHPage = thisHREF.split( &#8220;\/&#8221; ); <\/p>\n<p>thisHPage = tmpHPage[ tmpHPage.length-1 ]; <\/p>\n<p>tmpUPage = thisURL.split( &#8220;\/&#8221; ); <\/p>\n<p>thisUPage = tmpUPage[ tmpUPage.length-1 ]; <\/p>\n<p>strwrite = &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisHPage: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisHPage + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>strwrite += &#8220;&lt;tr&gt;&lt;td valign=top&gt;thisUPage: &lt;\/td&gt;&lt;td&gt;[&#8221; + thisUPage + &#8220;]&lt;\/td&gt;&lt;\/tr&gt;&#8221; <\/p>\n<p>document.write( strwrite ); <\/p>\n<p>&lt;\/script&gt;&lt;tr&gt;&lt;td&gt;<\/p>\n<p>\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d\uff1d<br \/>\u83b7\u53d6IP<br \/>1\u3001ASP.NET\u4e2d\u83b7\u53d6<\/p>\n<p>\u83b7\u53d6\u670d\u52a1\u5668\u7684IP\u5730\u5740\uff1a <br \/>using System.Net; <\/p>\n<p>string myIP,myMac;<br \/>System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; <br \/>if ( addressList.Length&gt;1) <br \/>{<br \/>myIP = addressList[0].ToString(); <br \/>myMac = addressList[1].ToString(); <br \/>} <br \/>else <br \/>{ <br \/>myIP = addressList[0].ToString(); <br \/>myMac = &#8220;\u6ca1\u6709\u53ef\u7528\u7684\u8fde\u63a5&#8221;\uff1b<br \/>} <br \/>myIP\u5730\u5740\u5c31\u662f\u670d\u52a1\u5668\u7aef\u7684ip\u5730\u5740\u3002<\/p>\n<p>\u83b7\u53d6\u5ba2\u6237\u7aef\u7684ip\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528<\/p>\n<p>\/\/\u83b7\u53d6\u767b\u5f55\u8005ip\u5730\u5740<br \/>string ip = Request.ServerVariables[&#8220;REMOTE_ADDR&#8221;].ToString(); <br \/>2\u3001\u901a\u8fc7JS\u83b7\u53d6<br \/>&lt;html&gt;<br \/>&lt;head&gt;<br \/>&lt;title&gt;&lt;\/title&gt;<br \/>&lt;meta http-equiv=&#8221;Content-Type&#8221; content=&#8221;text\/html; charset=gbk&#8221;&gt;<br \/>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>&lt;object classid=&#8221;CLSID:76A64158-CB41-11D1-8B02-00600806D9B6&#8243; id=&#8221;locator&#8221; style=&#8221;display:none;visibility:hidden&#8221;&gt;&lt;\/object&gt;<br \/>&lt;object classid=&#8221;CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223&#8243; id=&#8221;foo&#8221; style=&#8221;display:none;visibility:hidden&#8221;&gt;&lt;\/object&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/p>\n<p>&lt;form name=&#8221;myForm&#8221;&gt;<br \/>&lt;br\/&gt;MAC\u5730\u5740\uff1a&lt;input type=&#8221;text&#8221; name=&#8221;macAddress&#8221;&gt;<br \/>&lt;br\/&gt;IP\u5730\u5740\uff1a&lt;input type=&#8221;text&#8221; name=&#8221;ipAddress&#8221;&gt;<br \/>&lt;br\/&gt;\u4e3b\u673a\u540d\uff1a&lt;input type=&#8221;text&#8221; name=&#8221;hostName&#8221;&gt;<br \/>&lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<br \/>&lt;\/html&gt;<br \/>&lt;script language=&#8221;javas<wbr>cript&#8221;&gt;<br \/>var sMacAddr=&#8221;&#8221;;<br \/>var sIPAddr=&#8221;&#8221;;<br \/>var sDNSName=&#8221;&#8221;;<\/p>\n<p>var service = locator.ConnectServer();<br \/>service.Security_.ImpersonationLevel=3;<br \/>service.InstancesOfAsync(foo, &#8216;Win32_NetworkAdapterConfiguration&#8217;);<\/p>\n<p>&lt;\/script&gt;<\/p>\n<p>&lt;script FOR=&#8221;foo&#8221; EVENT=&#8221;On<wbr>ObjectReady(objObject,objAsyncContext)&#8221; LANGUAGE=&#8221;JScript&#8221;&gt;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(objObject.IPEnabled != null &amp;&amp; objObject.IPEnabled != &#8220;undefined&#8221; &amp;&amp; objObject.IPEnabled == true){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(objObject.IPEnabled &amp;&amp; objObject.IPAddress(0) !=null &amp;&amp; objObject.IPAddress(0) != &#8220;undefined&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sIPAddr = objObject.IPAddress(0);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(objObject.MACAddress != null &amp;&amp;objObject.MACAddress != &#8220;undefined&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sMacAddr = objObject.MACAddress;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(objObject.DNSHostName != null &amp;&amp;objObject.DNSHostName != &#8220;undefined&#8221;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sDNSName = objObject.DNSHostName;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&lt;\/script&gt;<\/p>\n<p>&lt;script FOR=&#8221;foo&#8221; EVENT=&#8221;On<wbr>Completed(hResult,pErrorObject, pAsyncContext)&#8221; LANGUAGE=&#8221;JScript&#8221;&gt;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myForm.macAddress.value=sMacAddr;<br \/>myForm.ipAddress.value=sIPAddr;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myForm.hostName.value=sDNSName;<br \/>&lt;\/script&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;asp.net\u3001js\u4e2d\u83b7\u53d6URL\u548cIP\u5730\u5740\u65b9\u6cd5\u5927\u5168 \u6536\u85cf HttpContext.Current. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-644","post","type-post","status-publish","format-standard","hentry","category-1","category-oldblog"],"views":533,"_links":{"self":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=644"}],"version-history":[{"count":1,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":748,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=\/wp\/v2\/posts\/644\/revisions\/748"}],"wp:attachment":[{"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ntxz.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}