asp.net(C#) 如何截取ip地址

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net(C#) 如何截取ip地址相关的知识,希望对你有一定的参考价值。

例如,获得的 ip 地址为 202.34.66.212
我想在页面显示 202.34.66.*

asp.net(C#) 应该怎么做呢?

方法很多,就是处理字符串的问题
原理:首先查找字符串最后一个"."的位置,然后再删除最后一个点后面的字符再加个"*"
举例:
string ip = Request.UserHostAddress;
ip=ip.Remove(ip.LastIndexOf('.')+1)+"*";
Response.Write(ip);

以上本机输出结果为127.0.0.*

当然你要不用Remove而用SubString截取最后个"."前的字符串加*也是可以的。
甚至你可以写个for语句再来一个一个字符的读,发现有三个"."后就不读了,然后+"*",
甚至你还可以从字符后面读起碰到一个"."就跳出...
提供些思路自己领悟吧,C#处理字符串很强的
参考技术A 这不就是一个字符的操作嘛
...
string strIP = "202.34.66.212";
int position = strIP.LastIndexOf(".");
string result = strIP.SubString(0, position + 1) + @".*";
...
大概就是这个思路,呵呵,你可以再修改下下 :)

推荐一些资料你,参考一下:

ASP.NET1.x/2.0电子资料汇总:

O'Reilly Silverlight 1.1简介
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163145
Wrox出版 Silverlight 1.0 (彩页染色代码、全面解析)
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=165953
Silverlight 1.0 Development with javascript
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163268
Sams出版 Silverlight 1.0 Unleashed
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162975
Silverlight and ASP.NET Revealed
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=174174
O'Reilly Essential Silverlight
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163266
XAML简明教程 CHM+PDF版
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163430
Pro ASP.NET 3.5 in C# 2008
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166640
Beginning ASP.NET 3.5 in VB 2008 从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166774
Wrox ASP.NET 2.0 MVP Hacks and Tips
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163467
Professional ASP.NET.2.0 Design
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160236
Professional Web Parts and Custom Controls with ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?BoardID=123&ID=162787
ASP.NET2.0入门经典
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146151
ASP.NET 2.0 技术内幕 微软技术丛书 清华大学出版
http://bbs.topsage.com/dispbbs_123_171293_1.html
Wrox ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145208
Beginning ASP.NET 2.0 in C# 2005 From Novice to Professional
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145396
Wrox Professional ASP.NET 2.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145202
Wrox Professional ASP.NET 2.0 XML
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145197
Wrox Professional ASP.NET 2.0 Security Membership and Role Management
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145198
Wrox Beginning ASP.NET 2.0 and Databases
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=147078
Pro ASP.NET 2.0 in VB 2005
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=173790
ASP.NET开发人员手册
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145391
ASP.NET 2.0网络编程入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145342
ASP.NET Web应用程序开发新思维
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145302
ASP.NET 2.0高级应用程序设计专家教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146379
ASP.NET XML高级编程 C#编程篇
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148800
ASP.NET程序开发 C#篇
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159285
ASP.NET XML深入编程技术
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148795
ASP.NET 2.0 Cookbook
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=151984
ASP.NET 2.0 Everyday Apps for Dummies
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=158103
Pro ASP.NET for SQL Server
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=167279
ASP.NET 从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=159331
Wrox Beginning ASP.NET 1.1 with Visual C#.NET 2003
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160240
ASP.NET 2.0 揭秘
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160249
Build Your Own ASP.NET 2.0 Web Site Using C# and VB
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163143
开发Microsoft ASP.NET 2.0网络应用程序
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160473
Pro ASP.NET 2.0 Website Programming
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=174179
开发ASP.NET 2.0 核心参考
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160474
Building Websites with VB.NET and DotNetNuke 4
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160639
Wrox出版 Professional DotNetNuke 4.0
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=166008
Professional DotNetNuke ASP.NET Portals
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163399

Ajax,ASP.NET Ajax电子资料汇总:

Ajax基础教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144263
Ajax宝典
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=148421
Wrox Beginning Ajax
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146082
Ajax in Practice
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=145551
Ajax模式最佳实践教程
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146080
Wrox Professional Rich Internet Applications AJAX and Beyond
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=160608
O'Reilly Ajax on Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160600
Practical JavaScript DOM Scripting and Ajax Projects
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=150277
Creating Web Pages with Asynchronous Javascript and XML
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162038
O'Reilly Securing Ajax Applications
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=162009
Advanced Ajax Architecture and Best Practices
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163068
Beginning ASP.NET 2.0 AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=151273
Introducing Microsoft ASP.NET AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=144519
Wrox Professional ASP.NET 2.0 AJAX
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=146172
O'Reilly Programming ASP.NET Ajax
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=172105
ASP.NET AJAX Programmer's Reference
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=163394

ASP.NET获取客户端IP及MAC地址

朋友最近问如何获取客户端IP及MAC地址,一直想把这段给整理一下,契机来了;下边分为了C#后台获取的方法和前台Javascript(调用ActiveX)获取的方法,大家如果有好的方法一起讨论撒O(∩_∩)O


C#代码获取:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;

namespace MyTest

    public partial class mac : System.Web.UI.Page
    
        [DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);


        protected void Page_Load(object sender, EventArgs e)
        
            GetMac(); 
        

        //获取客户端IP
        private string GetClientIP()
        
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == String.Empty)
            
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            

            if (null == result || result == String.Empty)
            
                result = HttpContext.Current.Request.UserHostAddress;
            
            return result;
        



        //获取MAC地址
        private void GetMac()
        
            // 在此处放置用户代码以初始化页面
            try
            
                string userip = Request.UserHostAddress;
                string strClientIP = Request.UserHostAddress.ToString().Trim();
                Int32 ldest = inet_addr(strClientIP); //目的地的ip
                Int32 lhost = inet_addr("");   //本地服务器的ip
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");
                if (mac_src == "0")
                
                    if (userip == "127.0.0.1")
                        Response.Write("正在访问Localhost!");
                    else
                        Response.Write("欢迎来自IP为" + userip + "的朋友!" + "
");
                    return;
                

                while (mac_src.Length < 12)
                
                    mac_src = mac_src.Insert(0, "0");
                

                string mac_dest = "";

                for (int i = 0; i < 11; i++)
                
                    if (0 == (i % 2))
                    
                        if (i == 10)
                        
                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                        
                        else
                        
                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                        
                    
                

                Response.Write("欢迎来自IP为" + userip + "
" + ",MAC地址为" + mac_dest + "的朋友!"

                 + "
");
            
            catch (Exception err)
            
                Response.Write(err.Message);
            


        
    

Javascript获取:

<HTML><HEAD><TITLE>WMI Scripting HTMLTITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
 document.forms[0].txtMACAddr.value=unescape(MACAddr);
 document.forms[0].txtIPAddr.value=unescape(IPAddr);
 document.forms[0].txtDNSName.value=unescape(sDNSName);
 //document.formbar.submit();
  SCRIPT>

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
   if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
   
    if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
    MACAddr = objObject.MACAddress;
    if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
    IPAddr = objObject.IPAddress(0);
    if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
    sDNSName = objObject.DNSHostName;
    
  SCRIPT>

<META content="MSHTML 6.00.2800.1106" name=GENERATOR>HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT>OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223>OBJECT>
<SCRIPT language=JScript>
   var service = locator.ConnectServer();
   var MACAddr ;
   var IPAddr ;
   var DomainAddr;
   var sDNSName;
   service.Security_.ImpersonationLevel=3;
   service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
   SCRIPT>

<FORM id=formfoo name=formbar action=NICPost.asp method=post><INPUT value=00:05:5D:0E:C7:FA name=txtMACAddr> <INPUT value=192.168.0.2 name=txtIPAddr> <INPUT value=typ name=txtDNSName> FORM>BODY>HTML>

 

以上是关于asp.net(C#) 如何截取ip地址的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 C# 和 ASP.NET 从网页中截取 div?

C# asp.net获取客户端的Ipv4 ip地址[重复]

asp.net C# 获取本机所有ip地址

如何在asp.net中获取IP地址

如何将c#做好的asp.net网站部署到iis上

在 ASP.NET 中获取服务器的 IP 地址?