ADSL的所有操作都要调用RASAPI32.DLL这个dll,详细的调用方法和参数,大家可以查询MSDN。
在codeplex上有个叫dotras的项目http://dotras.codeplex.com/这个项目封装了rasapi dll的操作,大家可以下载下来使用,不用自己在封装了,关于操作方法可以参见实例,下载过来后有示例的
List<string> historyIps = new List<string>(); private void ChangeIP() { SysConfig.IsChangeIp = true; QQHelper.WriteLog("更换IP"); HANDUPCON: string oldIpAddress = ""; RasConnection oldConn = null; GetIPAddress(out oldIpAddress, out oldConn); string entryName = ""; if (oldConn != null) { entryName = oldConn.EntryName; RasIPInfo ipAddresses = (RasIPInfo)oldConn.GetProjectionInfo(RasProjectionType.IP); string oldIp = ipAddresses.IPAddress.ToString(); ipAddresses = null; QQHelper.WriteLog("现在的IP是" + oldIp); bool isUp = false; try { Console.WriteLine("开始挂断"); oldConn.HangUp(10 * 1000); Thread.Sleep(SysConfig.RasHangUpSleepTime); if (RasConnection.GetActiveConnectionById(oldConn.EntryId) != null) { Console.WriteLine("结束挂断失败,重新挂断"); goto HANDUPCON; } oldConn = null; Console.WriteLine("结束挂断"); isUp = true; } catch (Exception ex) { QQHelper.WriteLog("宽带连接挂断失败," + ex.Message); } } CHANGEIP: try { var dt = SqliteHelper.GetDataTable("select * from sys_config"); RasDialer rs = new RasDialer(); if (entryName == "") { entryName = dt.Rows[0]["SC_NetEntryName"].ToString(); } rs.EntryName = entryName; rs.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); rs.Credentials = new NetworkCredential(dt.Rows[0]["SC_NetUser"].ToString(), dt.Rows[0]["SC_NetPwd"].ToString()); dt.Dispose(); rs.Dial(); rs.Dispose(); } catch (Exception ex) { QQHelper.WriteLog("宽带连接拨号失败," + ex.Message); Thread.Sleep(SysConfig.DialFaildSleepTime); goto CHANGEIP; } if (oldConn != null) { string ipAddresses = ""; GetIPAddress(out ipAddresses, out oldConn); oldConn = null; if (oldIpAddress == ipAddresses) { QQHelper.WriteLog("IP和上次重复,重新拨号"); Thread.Sleep(SysConfig.DialFaildSleepTime); goto HANDUPCON; } else { if (historyIps.Contains(ipAddresses)) { QQHelper.WriteLog("IP和前" + SysConfig.IPLoopCount + "次重复,重新拨号"); Thread.Sleep(SysConfig.DialFaildSleepTime); goto HANDUPCON; } if (historyIps.Count >= SysConfig.IPLoopCount) { historyIps.RemoveAt(0); historyIps.Add(ipAddresses); } historyIps.Add(ipAddresses); } QQHelper.WriteLog("现在的IP是" + ipAddresses); } SysConfig.IsChangeIp = false; } //切换IP操作 private void GetIPAddress(out string ipAddress, out RasConnection oldConn) { oldConn = null; ipAddress = ""; var conns = RasConnection.GetActiveConnections(); foreach (var conn in conns) { if (conn.Device.DeviceType.ToLower() == "pppoe") { oldConn = conn; break; } } conns = null; if (oldConn != null) { try { RasIPInfo ipAddresses = (RasIPInfo)oldConn.GetProjectionInfo(RasProjectionType.IP); ipAddress = ipAddresses.IPAddress.ToString(); ipAddresses = null; } catch { } } } //获取连接信息
我之前找过一次用API实现ADSL拨号的资料,结果发现没那么简单。网上很多源代码 是很老的,根本就用不上。
后来发现了系统命令就自带命令可实现ADSL拨号。
所以这里再贴一个方式:
rasdial.exe 宽带名称 宽带帐号 宽带密码
说明:
rasdial.exe ---> 是windows命令
宽带名称 ---> 是创建的拨号连接的名称
宽带帐号 --->ISP给你的帐号
宽带密码 --->ISP给你的密码
示例:
运行--cmd.exe--输入下面的命令 自己修改
rasdial.exe 宽带连接 0769123456@163.gd dg123456
-----------------以上是宽带连接---------------------------
rasdial.exe 宽带名称 /disconnect
-----------------以上是断开连接---------------------------
其实自己用代码来实现的话,就能实现自动换IP刷点赞或投票神马的。不过效率有多高就不知道了。