制作一个简易的登陆客户端

为了解决校园网客户端的各种限制,所以自己写了一个简易的登陆客户端

根据上一篇文章联通校园网不能开热点问题解决办法,获取的数据写了下面的程序。

如下图:




这里直接用现成DotRas对PPPoE软件进行操作

一、核心的三个操作

1、

// 创建或更新一个PPPOE连接(指定PPPOE名称)
        void CreateOrUpdatePPPOE(string updatePPPOEname)
        {
            RasDialer dialer = new RasDialer();
            RasPhoneBook allUsersPhoneBook = new RasPhoneBook();
            string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
            allUsersPhoneBook.Open(path);
            // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址
            if (allUsersPhoneBook.Entries.Contains(updatePPPOEname))
            {
                allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " ";
                // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用
                allUsersPhoneBook.Entries[updatePPPOEname].Update();
            }
            // 创建一个新PPPOE
            else
            {
                string adds = string.Empty;
                ReadOnlyCollection<RasDevice> readOnlyCollection = RasDevice.GetDevices();
                //                foreach (var col in readOnlyCollection)
                //                {
                //                    adds += col.Name + ":" + col.DeviceType.ToString() + "|||";
                //                }
                //                _log.Info("Devices are : " + adds);
                // Find the device that will be used to dial the connection.
                RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First();
                RasEntry entry = RasEntry.CreateBroadbandEntry(updatePPPOEname, device);    //建立宽带连接Entry
                entry.PhoneNumber = " ";
                allUsersPhoneBook.Entries.Add(entry);
            }
        }

2、

//宽带连接,成功返回true,失败返回 false
        /// <param name="PPPOEname">宽带连接名称</param>
        /// <param name="username">宽带账号</param>
        /// <param name="password">宽带密码</param>
        /// <returns></returns>
        public bool Connect(string PPPOEname, string username, string password,ref string msg )
        {
            try
            {
                CreateOrUpdatePPPOE(PPPOEname);
                using (RasDialer dialer = new RasDialer())
                {
                    dialer.EntryName = PPPOEname;
                    dialer.AllowUseStoredCredentials = true;
                    dialer.Timeout = 1000;
                    dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);
                    dialer.Credentials = new NetworkCredential(username, password);
                    dialer.Dial();
                    return true;
                }
            }
            catch (RasException re)
            {
                msg = re.ErrorCode + " " + re.Message;
                return false;
            }
        }

3、

//断开 宽带连接
        public void Disconnect()
        {
            ReadOnlyCollection<RasConnection> conList = RasConnection.GetActiveConnections();
            foreach (RasConnection con in conList)
            {
                con.HangUp();
            }
        }

二、根据上一篇文章,调用这三个操作如下

           private void lianjie() {
            //连接
            String m = "";
            String uname = skinTextBox1.Text;
            String pswd = skinTextBox2.Text;
            String username = uname + "@dxcwo201";
            String password = (char)1 + pswd;
            CreateOrUpdatePPPOE("宽带连接");
            bool b = Connect("宽带连接", username, password, ref m);
            if (b)
            {
                panel1.Visible = true;
                label1.Text = "";
            }
            else {
                label1.Text = "登陆失败!";
            }
        }

这里已基本实现网络的新建,连接,断开

当然界面也不能太low,下面对界面一些优化

首先,扁平化,将窗体边框设为零

设为0后解决窗体移动的问题

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        public Login()
        {
            InitializeComponent();
            //去边框后,实现窗体的移动
            foreach (var item in this.Controls)
            {
                if ((item as GroupBox) != null)
                {
                    (item as GroupBox).MouseDown += Login_MouseDown;
                }
                else if ((item as Panel) != null)
                {
                    (item as Panel).MouseDown += Login_MouseDown;
                }
            }
        }
        //去边框后,实现窗体的移动
        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        private void skinPictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

然后是右上角的最小化,关闭

        //窗体最小化
        private void button3_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        //窗体退出
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }




可以开wifi,用虚拟机的客户端:下载链接 下载链接2


©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页