45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:如何使用C#adsl拨号刷网站流量?

如何使用C#adsl拨号刷网站流量?

2016-08-26 20:31:20 来源:www.45fan.com 【

如何使用C#adsl拨号刷网站流量?

好久没更新blog了!新年到了,各公司网站到这个时候都大规模的跳水。推广部给出了任务,年假咱们就刷些ip吧。好主意,那就行动吧!

由于公司内部用就没写哪么复杂!给大家参考下

form1.cs

namespace mainApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
private Thread t;
private string[] aUrl;
private System.Windows.Forms.Timer timer1;
private int tv = 20;
private System.ComponentModel.IContainer components;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(272, 176);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "请输入网址";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 16);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(256, 152);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "http://www.changshang.com";
//
// button1
//
this.button1.Location = new System.Drawing.Point(128, 192);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(70, 24);
this.button1.TabIndex = 1;
this.button1.Text = "开始";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(208, 192);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(70, 24);
this.button2.TabIndex = 2;
this.button2.Text = "停止";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(8, 200);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(66, 17);
this.label1.TabIndex = 3;
this.label1.Text = "定时(秒):";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(72, 193);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(48, 21);
this.textBox2.TabIndex = 4;
this.textBox2.Text = "20";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 238);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1";
this.Text = "刷流量";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
}

private void button1_Click(object sender, System.EventArgs e)
{
try
{
if(chkTxtUrl()==false)
return;
if(chkTimerVal()==false)
return;
else
timer1.Interval = tv*1000;
timer1.Start();
}
catch{}
finally
{
btnStatsChange();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
try
{
timer1.Stop();
//t.Abort();
}
catch
{
}
finally
{
btnStatsChange();
}
}
#region 自定义函数
/// <summary>
/// 当出发按扭事件时改变按扭状态
/// </summary>
private void btnStatsChange()
{
this.button1.Enabled = (!this.button1.Enabled);
this.button2.Enabled = (!this.button2.Enabled);
}
public bool chkTimerVal()
{
if(textBox2.Text.Trim()=="")
{
MessageBox.Show("请输入刷新时间。");
return false;
}
try
{
tv = int.Parse(textBox2.Text.Trim());
if(tv<20)
{
MessageBox.Show("最小时间间隔为20秒,请重新设置时间。");
return false;
}
}
catch
{
MessageBox.Show("请输入一个整数值。");
return false;
}
return true;
}
public void openNewWin()
{
timer1.Stop();
//重新进行adsl拨号。
int tmp =0;
RASHelper.InternetDial(IntPtr.Zero,"",2,ref tmp,0);
if(RASHelper.IsNetConnectViaLAN()==false)
openNewWin();
t = new Thread(new ThreadStart(_openNewWin));
t.IsBackground = true;
t.Start();
timer1.Start();
}
/// <summary>
/// 启动一新的浏览器窗体,刷新用户设定的网址
/// </summary>
public void _openNewWin()
{
System.Diagnostics.Process p = System.Diagnostics.Process.Start("iexplore.exe","http://www.changshang.com/?id=refresh");
System.Threading.Thread.Sleep(10000);
p.CloseMainWindow();
p.Close();
//线程结束时关闭所有adsl连接。
RASHelper.HangUp();
}
/// <summary>
/// 判断用户输入的网址是否符合要求,返回bool值
/// </summary>
/// <returns></returns>
public bool chkTxtUrl()
{
if(this.textBox1.Text.Trim()=="")
{
MessageBox.Show("请输入网址。");
return false;
}
aUrl = this.textBox1.Text.Split('/n');
Regex re = new Regex(@"http://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?");

for(int i = 0; i < aUrl.Length; i++)
{
if(re.IsMatch(aUrl[i])==false)
{
MessageBox.Show("您输入的网址有错误。");
return false;
}
}
return true;
}
#endregion

private void timer1_Tick(object sender, System.EventArgs e)
{
openNewWin();
}
}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

RASHelper.cs

using System;
using System.Collections;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.ComponentModel;

namespace mainApp
{
/// <summary>
/// RASHelper 的摘要说明。
/// </summary>
public class RASHelper
{
public RASHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
const int INTERNET_RAS_INSTALLED = 0x10;
const int INTERNET_CONNECTION_LAN = 0x02;

[DllImport("WININET", CharSet = CharSet.Auto)]
public static extern bool InternetGetConnectedState(
ref int lpdwFlags,
int dwReserved);
[DllImport("WININET",CharSet = CharSet.Auto)]
public static extern int InternetGetConnectedStateEx(
ref int lpdwFlags,
string lpszConnectionName,
int dwNameLen,
int dwReserved);

const int INTERNET_DIALSTATE_DISCONNECTED = 1;
const int INTERNET_AUTODIAL_FORCE_ONLINE = 1;
const int INTERNET_AUTODIAL_FORCE_UNATTENDED = 2;
const int INTERNET_DIAL_UNATTENDED = 0x8000;

[DllImport("WININET",CharSet = CharSet.Auto)]
public static extern int InternetDial(
IntPtr hwndParent,
string lpszConnectoid,
int dwFlags,
ref int lpdwConnection,
int dwReserved
);

const int MAX_PATH = 260;
const int RAS_MaxDeviceType = 16;
const int RAS_MaxPhoneNumber = 128;
const int RAS_MaxEntryName = 256;
const int RAS_MaxDeviceName = 128;

const int RAS_Connected = 0x2000;

[DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasEnumConnections(
ref RASCONN lprasconn,
ref int lpcb,
ref int lpcConnections);

[DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasGetConnectStatus(
IntPtr hrasconn,
ref RASCONNSTATUS lprasconnstatus);
[DllImport("RASAPI32", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int RasHangUp(IntPtr hrasconn);

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONN
{
public int dwSize;
public IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
public string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceType + 1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceName + 1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szPhonebook;
public int dwSubEntry;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct RASCONNSTATUS
{
public int dwSize;
public int rasconnstate;
public int dwError;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceType + 1)]
public string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceName + 1)]
public string szDeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPhoneNumber + 1)]
public string szPhoneNumber;
}
/// <summary>
/// 关闭拨号连接
/// </summary>

public static void HangUp()
{
IntPtr m_ConnectedRasHandle;
RASCONN lprasConn = new RASCONN();

lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
lprasConn.hrasconn = IntPtr.Zero;
int lpcb = 0;
int lpcConnections = 0;
int nRet = 0;
lpcb = Marshal.SizeOf(typeof(RASCONN));


nRet = RasEnumConnections(ref lprasConn, ref lpcb, ref
lpcConnections);


if(nRet != 0)
{
return;

}
if(lpcConnections > 0)
{
RASCONNSTATUS rasconnstatus = new RASCONNSTATUS();
m_ConnectedRasHandle = lprasConn.hrasconn;
RasHangUp(m_ConnectedRasHandle);
Thread.Sleep(1000);
for(;;)
{
rasconnstatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS));
if(RasGetConnectStatus(m_ConnectedRasHandle,ref rasconnstatus)==0) //获得当前连接的状态
{
if(rasconnstatus.rasconnstate==0x2001) //已经彻底挂断
break;
}
else
break;//获得当前连接状态失败
}
}

}
///<sumarry>
///判断是否是lan连接
///</sumarry>
public static bool IsNetConnectViaLAN()
{
int dwFlags = 0;
return InternetGetConnectedState(ref dwFlags,0);
//return dwFlags==INTERNET_CONNECTION_LAN;
}
}
}

 


本文地址:http://www.45fan.com/a/question/68099.html
Tags: 网站 拨号 C#adsl
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部