我想有很多人想用不同的IP點擊一個鏈接吧,想多提升點廣告的點擊量吧(呵呵,你別有用心的話後果自負),今天做個使用代理IP來點擊鏈接的小軟件,測試過,一般情況下沒問題。
大家不想以為這是多麼高深的技術,或者多麼復雜的技術。簡單來說,C#4句話搞定:
WebClient wc = new WebClIEnt();
WebProxy proxy = new WebProxy(ip);
wc.Proxy = proxy;
wc.DownloadData(link);
呵呵,但是小工具成型的話,這幾句話就不OK啦。基本思路就是用多線程用N個IP去循環處理N個鏈接。但這個過程需要一段時間,因此用單線程的話會造成程序長時間未響應,Windows會報錯。
在這個小工具中,需要處理IP數組和鏈接數組,稍微人性化一點,加個進度條。這裡唯一個小難點就是非創建控件線程訪問該控件會拋出異常。
ThreadStart ts = new ThreadStart(Run);
Thread t = new Thread(ts);
t.Start();
用一個新線程啟動主要模塊
void Run()
{
progressBar1.Value++;
}
問題出現了,運行到progressBar1.Value++時異常就出現了,如上所述.
解決辦法很簡單:
public delegate void ProgressBar1InitInvokeDelegate();
public void ChangeProgressBarValue()
{
if (progressBar1.InvokeRequired == true)
{
progressBar1.BeginInvoke(new ProgressBar1InvokeDelegate(ChangeProgressBarValue));
}
else
{
progressBar1.Value++;
}
}
void Run()
{
ChangeProgressBarValue();
}
接下來還想什麼,網上找些代理IP去試試吧,祝您一路“點”通!:)
第一次,不會上傳包,代碼貼在下面:
.cs文件
namespace WebClicker
...{
public partial class Clicker : Form
...{
= State =#region = State =
private ProcessState _state = ProcessState.Stop;
public ProcessState State
...{
get ...{ return _state; }
set ...{ _state = value; }
}
#endregion
public delegate void Button2InvokeDelegate(string text);
public delegate void ProgressBar1InvokeDelegate();
public delegate void ProgressBar1InitInvokeDelegate(int value);
public delegate void lblInfoInvokeDelegate(string text);
public Clicker()
...{
InitializeComponent();
}
public enum ProcessState
...{
Begin = 0,
Stop = 1
}
public void ChangelblInfoText(string text)
...{
if (lblInfo.InvokeRequired == true)
...{
lblInfo.BeginInvoke(new lblInfoInvokeDelegate(ChangelblInfoText), new object[] ...{ text });
}
else
...{
lblInfo.Text = text;
}
}
public void ChangeButton2Text(string text)
...{
if (button2.InvokeRequired == true)
...{
button2.BeginInvoke(new Button2InvokeDelegate(ChangeButton2Text), new object[] ...{ text });
}
else
...{
button2.Text = text;
}
}
public void InitprogressBar(int value)
...{
if (progressBar1.InvokeRequired == true)
...{
progressBar1.BeginInvoke(new ProgressBar1InitInvokeDelegate(InitprogressBar), new object[] ...{ value });
}
else
...{
progressBar1.Minimum = 0;
progressBar1.Maximum = value;
progressBar1.Step = 1;
progressBar1.Visible = true;
progressBar1.Value = 0;
}
}
public void ChangeProgressBarValue()
...{
if (progressBar1.InvokeRequired == true)
...{
progressBar1.BeginInvoke(new ProgressBar1InvokeDelegate(ChangeProgressBarValue));
}
else
...{
progressBar1.Value++;
}
}
public void EndProgressBar()
...{
if (progressBar1.InvokeRequired == true)
...{
progressBar1.BeginInvoke(new ProgressBar1InvokeDelegate(EndProgressBar));
}
else
...{
progressBar1.Value = 0;
progressBar1.Visible = false;
}
}
private void button1_Click(object sender, EventArgs e)
...{
string temp = Utility.OpenDialog();
if (string.IsNullOrEmpty(temp) == false)
...{
txtFile.Text = temp;
}
}
private void button4_Click(object sender, EventArgs e)
...{
string temp = Utility.OpenDialog();
if (string.IsNullOrEmpty(temp) == false)
...{
txtLinkFile.Text = temp;
}
}
private void button2_Click(object sender, EventArgs e)
...{
if (button2.Text.Equals("運行"))
...{
ChangelblInfoText("正在點擊...");
}
}
ThreadStart ts = new ThreadStart(Run);
Thread t = new Thread(ts);
t.Start();
}
else
...{
ChangelblInfoText("正在停止點擊...");
this.State = ProcessState.Stop;
}
}
void Run()
...{
this.State = ProcessState.Begin;
if (string.IsNullOrEmpty(txtFile.Text.Trim()) || string.IsNullOrEmpty(txtLinkFile.Text.Trim())) return;
ChangeButton2Text("停止");
string temp = System.Text.RegularExpressions.Regex.Replace(Lihui.Common.FileHelper.ReadText(txtFile.Text), " ", "|");
string[] arrayIP = temp.Split(''|'');
InitprogressBar(arrayIP.Length);
temp = System.Text.RegularExpressions.Regex.Replace(Lihui.Common.FileHelper.ReadText(txtLinkFile.Text), " ", "|");
&nb string[] arrayLink = temp.Split(''|'');
List<string> errorIPList = new List<string>();
foreach (string ip in arrayIP)
...{
if (StopThread()) break;
ChangeProgressBarValue();
if (string.IsNullOrEmpty(ip.Trim())) continue;
WebClient wc = new WebClIEnt();
WebProxy proxy = new WebProxy(ip);
wc.Proxy = proxy;
foreach (string link in arrayLink)
...{
if (StopThread()) break;
if (string.IsNullOrEmpty(link.Trim())) continue;
try
...{
&n wc.DownloadData(link);
ChangelblInfoText(string.Format("{0}點擊{1}一次,正在點擊...", ip, link));
}
catch
...{
errorIPList.Add(ip);
}
}
}
foreach (string errip in errorIPList)
...{
Lihui.Common.FileHelper.WriteText(Environment.CurrentDirectory + "/ErrorIP/errorIp.txt", errip + Const.ENTER_CHAR, true);
}
}
bool StopThread()
...{
if (this.State == ProcessState.Stop)
...{
ChangeButton2Text("運行");
ChangelblInfoText("已停止");
EndProgressBar();
return true;
}
return false;
}
}
}.designer.cs文件:
namespace WebClicker
...{
partial class Clicker
...{
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
this.button4 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.txtLinkFile = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.txtFile = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.lblInfo = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button4
//
this.button4.Location = new System.Drawing.Point(294, 53);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(75, 23);
this.button4.TabIndex = 12;
this.button4.Text = "浏覽";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 58);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(77, 12);
this.label2.TabIndex = 11;
this.label2.Text = "批量導入鏈接";
//
// txtLinkFile
//
this.txtLinkFile.Location = new System.Drawing.Point(106, 54);
this.txtLinkFile.Name = "txtLinkFile";
this.txtLinkFile.Size = new System.Drawing.Size(175, 21);
this.txtLinkFile.TabIndex = 10;
//
// button1
//
this.button1.Location = new System.Drawing.Point(294, 11);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "浏覽";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(65, 12);
this.label1.TabIndex = 8;
this.label1.Text = "批量導入IP";
//
// txtFile
//
this.txtFile.Location = new System.Drawing.Point(106, 12);
this.txtFile.Name = "txtFile";
this.txtFile.Size = new System.Drawing.Size(175, 21);
this.txtFile.TabIndex = 7;
//
// button2
//
this.button2.Location = new System.Drawing.Point(294, 89);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 13;
this.button2.Text = "運行";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(14, 89);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(267, 23);
this.progressBar1.TabIndex = 14;
this.progressBar1.Visible = false;
//
// lblInfo
//
this.lblInfo.Location = new System.Drawing.Point(14, 118);
this.lblInfo.Multiline = true;
this.lblInfo.Name = "lblInfo";
this.lblInfo.Size = new System.Drawing.Size(355, 60);
this.lblInfo.TabIndex = 16;
this.lblInfo.Text = "詳細信息提示";
//
// Clicker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClIEntSize = new System.Drawing.Size(382, 184);
this.Controls.Add(this.lblInfo);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button2);
&nb this.Controls.Add(this.button4);
this.Controls.Add(this.label2);
this.Controls.Add(this.txtLinkFile);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtFile);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "Clicker";
this.Text = "Clicker";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtLinkFile;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtFile;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.TextBox lblInfo;
}
}