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

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>

asp.net获取URL和IP地址的更多相关文章

  1. Asp.net获取用户真实Ip地址

    /// <summary> /// 获取远程访问用户的Ip地址 /// </summary> /// <returns>返回Ip地址</returns> ...

  2. asp.net 获取客户机IP地址

    /// <summary> ///get client IP /// </summary> /// <returns></returns> public ...

  3. asp.net和脚本获取当前的URL、IP地址

    介绍一下ASP.NET取得当前页面的完整URL 的方放,icech做成了函数,直接用吧! private string GetPath() { string strPath = "http: ...

  4. asp dotnet core 从 Frp 获取用户真实 IP 地址

    我在本地开一个服务,然后通过 Frp 让小伙伴可以在外网访问我的 API 连接,但是直接通过 RemoteIp 拿到的是本地的地址.本文告诉小伙伴如何通过 Frp 可以拿到用户的真实 IP 地址 我写 ...

  5. java获取客户端请求IP地址(公网ip)

    之前写了一个获取ip地址的方法,但是放网上一查显示此Ip地址是局域网ip地址,要是想获取请求端的真实公网ip地址怎么样了,看了一些别人的博客后发现,想要获取客户端的公网ip必须借助第三方. packa ...

  6. Java 获取Linux 的IP地址

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  7. Java获取客户端真实IP地址的两种方法

    在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的.但是在通过了Apache,Squid等反向代理软件就不能获取到客户端的真实I ...

  8. 微信公众号平台接口开发:基础支持,获取微信服务器IP地址

    官方说明 目前看不出来这个接口有哪些具体运用,但是既然有这个接口,那我们就试试能不能用 访问接口 修改WeCharBase.cs,新增以下2个方法 public static string Serve ...

  9. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

随机推荐

  1. struts 多文件上传 xml 版本

    [本文简介] 本文将介绍 以配置 struts.xml  的方式 实现 多文件上传的功能. [文件夹结构] [struts.xml] <?xml version="1.0" ...

  2. mongo 一次插入多条

    db.getCollection('organization').insert( [ {"organizationTitle" : "台州广播电台1", &qu ...

  3. POJ 2253 Frogger (Dijkstra变型)

    题意:求点1到点2的路径中,权值最大的那条边,其最小值是多少. 分析:最大值最小化.可以将迪杰斯特拉模板中的松弛操作加以修改,在O(n^2)的时间内解决该问题.其中需要注意的是,dist[i]指的是: ...

  4. 8月白盒测试课程 - C C++ 白盒测试实践

    8月白盒测试课程 - C C++ 白盒测试实践http://gdtesting.cn/news.php?id=36

  5. pig(转载)

    本文可以让刚接触pig的人对一些基础概念有个初步的了解.本文大概是互联网上第一篇公开发表的且涵盖大量实际例子的Apache Pig中文教程(由Google搜索可知),文中的大量实例都是作者Darran ...

  6. Java内置锁synchronized的可重入性

    学习自 https://blog.csdn.net/aigoogle/article/details/29893667 对我很有帮助 感谢作者

  7. 关于获得MFC窗口其它类指针的方法(csdn)

    转自:http://tieba.baidu.com/p/252804018 访问应用程序的其它类 获得CWinApp: -在CMainFrame,CChildFrame,CDocument,CView ...

  8. Web层辅助工具类

    Java web开发中经常用到的一些方法: import java.io.BufferedReader; import java.net.InetAddress; import java.net.Un ...

  9. JDK1.7之Fork/join

    Fork/Join框架是Java 7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.Fork/Join框架要完成两件事情: 1.任务分 ...

  10. 华为交换机S5700系列配置镜像端口(1:1)

    配置本地镜像口(1:1) 组网需求 如图一所示,某公司行政部通过Switch与外部Internet通信,监控设备Server与Switch直连. 现在希望通过Server对行政部访问Internet的 ...