C#获取路由器外网IP,MAC地址

C#实现的获取路由器MAC地址,路由器外网地址。对于要获取路由器MAC地址,一定需要知道路由器web管理系统的用户名和密码。至于获取路由器的外网IP地址,可以不需要知道路由器web管理系统的用户名和密码,但是需要有一个代理页面获取客户端公网ip地址的,这样C#请求此页面即可获取到路由器公网ip地址。如:
http://xxxx.getip.ashx
测试路由为水星 MR804,水星 MR808,都可以成功重启路由和获取到路由器MAC和外网IP地址

源代码下载地址:C#实现路由器重启更换IP,获取路由器MAC地址源代码

复制代码
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
public class Router
{
        Encoding gb2312 = Encoding.GetEncoding(936);//路由器的web管理系统默认编码为gb2312
        /// <summary>
        /// 使用HttpWebRequest对象发送请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding">编码</param>
        /// <param name="cache">凭证</param>
        /// <returns></returns>
        private static string SendRequest(string url, Encoding encoding,CredentialCache cache)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            if (cache != null)
            {
                request.PreAuthenticate = true;
                request.Credentials = cache;
            }
            string html = null;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader srd = new StreamReader(response.GetResponseStream(), encoding);
                html = srd.ReadToEnd();
                srd.Close();
                response.Close();
            }
            catch (Exception ex) { html = "FALSE" + ex.Message; }
            return html;
        }
        /// <summary>
        /// 获取路由MAC和外网IP地址
        /// </summary>
        /// <param name="RouterIP">路由IP地址,就是网关地址了,默认192.168.1.1</param>
        /// <param name="UserName">用户名</param>
        /// <param name="Passowrd">密码</param>
        /// <returns></returns>
        private string LoadMACWanIP(string RouterIP,string UserName,string Passowrd)
        {
            CredentialCache cache = new CredentialCache();
            string url = "http://" + RouterIP + "/userRpm/StatusRpm.htm";
            cache.Add(new Uri(url), "Basic", new NetworkCredential(UserName, Passowrd));
            return SendRequest(url, gb2312, cache);
        }
}
复制代码

 

 

出处:http://www.coding123.net/article/20120220/charp-get-router-wlan-ip-mac-address.aspx

 

posted on   jack_Meng  阅读(7671)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2014-07-14 WINDOWS下kill进程的命令

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩