网站投票程序
早前几个朋友需要帮忙投票,而现在的投票网站都基本上有个限制:一个ip一天只能头一次票。从网络上搜索到破解方法有两种:1、使用拨号网络接入,每次拨号成功后会自动分配ip;2、固定ip接入,可以采用代理方式,欺骗投票程序。基于这个思路做了两个版本的程序,下载地址
1、拨号网络接入。这个方式采用的思路,开启两个线程:第一个做一死循环,拨号,投票,断网;第二个线程轮询处理异常窗口。
该方法使用win API。同时使用网络上前辈写的RAD类进行拨号。按窗口名称搜索窗体,并置当前窗体和向窗体发送按键的win API调用
获取窗体句柄和发送按键声明
1 using System.Runtime.InteropServices;
2 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
3 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
4
5 [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
6 private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
7
8 [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
9 private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
10
11 [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
12 private static extern void SetForegroundWindow(IntPtr hwnd);
13
2 [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
3 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
4
5 [DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
6 private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
7
8 [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
9 private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
10
11 [DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
12 private static extern void SetForegroundWindow(IntPtr hwnd);
13
查找窗体句柄和发送按键方法
1 IntPtr hwnd = FindWindow(null, windowName); //查找窗体的句柄
2
3 if (hwnd != IntPtr.Zero)
4 {
5 SetForegroundWindow(hwnd);//将该句柄设为当前活动窗口
6 SendKeys.SendWait(key);
7
8 }
2
3 if (hwnd != IntPtr.Zero)
4 {
5 SetForegroundWindow(hwnd);//将该句柄设为当前活动窗口
6 SendKeys.SendWait(key);
7
8 }
参考: 键盘按键编码信息
网络牛人的调用windows拨号类,具体出自谁手不记得了。
调用windows拨号类
1
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
5 using System.Text;
6
7
8 namespace RAD
9 {
10
11 ///调用方法:
12 ///RASDisplay ras = new RASDisplay();
13 ///ras.Disconnect();//断开连接
14 ///ras.Connect("ADSL");//重新拨号
15 public struct RASCONN
16 {
17 public int dwSize;
18 public IntPtr hrasconn;
19 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
20 public string szEntryName;
21 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
22 public string szDeviceType;
23 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
24 public string szDeviceName;
25 }
26
27 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
28 public struct RasStats
29 {
30 public int dwSize;
31 public int dwBytesXmited;
32 public int dwBytesRcved;
33 public int dwFramesXmited;
34 public int dwFramesRcved;
35 public int dwCrcErr;
36 public int dwTimeoutErr;
37 public int dwAlignmentErr;
38 public int dwHardwareOverrunErr;
39 public int dwFramingErr;
40 public int dwBufferOverrunErr;
41 public int dwCompressionRatioIn;
42 public int dwCompressionRatioOut;
43 public int dwBps;
44 public int dwConnectionDuration;
45 }
46
47 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
48 public struct RasEntryName
49 {
50 public int dwSize;
51 //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
52 public string szEntryName;
53 //#if WINVER5
54 // public int dwFlags;
55 // [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
56 // public string szPhonebookPath;
57 //#endif
58 }
59 public class RAS
60 {
61 [DllImport("Rasapi32.dll", EntryPoint = "RasEnumConnectionsA",
62 SetLastError = true)]
63
64 internal static extern int RasEnumConnections
65 (
66 ref RASCONN lprasconn, // buffer to receive connections data
67 ref int lpcb, // size in bytes of buffer
68 ref int lpcConnections // number of connections written to buffer
69 );
70
71
72 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
73 internal static extern uint RasGetConnectionStatistics(
74 IntPtr hRasConn, // handle to the connection
75 [In, Out]RasStats lpStatistics // buffer to receive statistics
76 );
77 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
78 public extern static uint RasHangUp(
79 IntPtr hrasconn // handle to the RAS connection to hang up
80 );
81
82 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
83 public extern static uint RasEnumEntries(
84 string reserved, // reserved, must be NULL
85 string lpszPhonebook, // pointer to full path and
86 // file name of phone-book file
87 [In, Out]RasEntryName[] lprasentryname, // buffer to receive
88 // phone-book entries
89 ref int lpcb, // size in bytes of buffer
90 out int lpcEntries // number of entries written
91 // to buffer
92 );
93
94 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
95 public extern static int InternetDial(
96 IntPtr hwnd,
97 [In]string lpszConnectoid,
98 uint dwFlags,
99 ref int lpdwConnection,
100 uint dwReserved
101 );
102
103 public RAS()
104 {
105 }
106 }
107 public enum DEL_CACHE_TYPE //要删除的类型。
108 {
109 File,//表示internet临时文件
110 Cookie //表示Cookie
111 }
112
113 public class RASDisplay
114 {
115 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
116 public static extern bool DeleteUrlCacheEntry(
117 DEL_CACHE_TYPE type
118 );
119 private string m_duration;
120 private string m_ConnectionName;
121 private string[] m_ConnectionNames;
122 private double m_TX;
123 private double m_RX;
124 private bool m_connected;
125 private IntPtr m_ConnectedRasHandle;
126
127 RasStats status = new RasStats();
128 public RASDisplay()
129 {
130 m_connected = true;
131
132 RAS lpras = new RAS();
133 RASCONN lprasConn = new RASCONN();
134
135 lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
136 lprasConn.hrasconn = IntPtr.Zero;
137
138 int lpcb = 0;
139 int lpcConnections = 0;
140 int nRet = 0;
141 lpcb = Marshal.SizeOf(typeof(RASCONN));
142
143 nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, ref
144 lpcConnections);
145
146 if (nRet != 0)
147 {
148 m_connected = false;
149 return;
150
151 }
152
153 if (lpcConnections > 0)
154 {
155 //for (int i = 0; i < lpcConnections; i++)
156
157 //{
158 RasStats stats = new RasStats();
159
160 m_ConnectedRasHandle = lprasConn.hrasconn;
161 try
162 {
163 RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);
164 }
165 catch (System.Exception ex)
166 {
167 throw ex;
168 }
169 finally
170 {
171
172
173 m_ConnectionName = lprasConn.szEntryName;
174
175 int Hours = 0;
176 int Minutes = 0;
177 int Seconds = 0;
178
179 Hours = ((stats.dwConnectionDuration / 1000) / 3600);
180 Minutes = ((stats.dwConnectionDuration / 1000) / 60) - (Hours * 60);
181 Seconds = ((stats.dwConnectionDuration / 1000)) - (Minutes * 60) - (Hours * 3600);
182
183
184 m_duration = Hours + " hours " + Minutes + " minutes " + Seconds + " secs";
185 m_TX = stats.dwBytesXmited;
186 m_RX = stats.dwBytesRcved;
187 //}
188 }
189 }
190 else
191 {
192 m_connected = false;
193 }
194
195
196 int lpNames = 1;
197 int entryNameSize = 0;
198 int lpSize = 0;
199 RasEntryName[] names = null;
200
201 entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
202 lpSize = lpNames * entryNameSize;
203
204 names = new RasEntryName[lpNames];
205 names[0].dwSize = entryNameSize;
206
207 uint retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);
208
209 //if we have more than one connection, we need to do it again
210 if (lpNames > 1)
211 {
212 names = new RasEntryName[lpNames];
213 for (int i = 0; i < names.Length; i++)
214 {
215 names[i].dwSize = entryNameSize;
216 }
217
218 retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);
219
220 }
221 m_ConnectionNames = new string[names.Length];
222
223
224 if (lpNames > 0)
225 {
226 for (int i = 0; i < names.Length; i++)
227 {
228 m_ConnectionNames[i] = names[i].szEntryName;
229 }
230 }
231 }
232
233 public string Duration
234 {
235 get
236 {
237 return m_connected ? m_duration : "";
238 }
239 }
240
241 public string[] Connections
242 {
243 get
244 {
245 return m_ConnectionNames;
246 }
247 }
248
249 public double BytesTransmitted
250 {
251 get
252 {
253 return m_connected ? m_TX : 0;
254 }
255 }
256 public double BytesReceived
257 {
258 get
259 {
260 return m_connected ? m_RX : 0;
261
262 }
263 }
264 public string ConnectionName
265 {
266 get
267 {
268 return m_connected ? m_ConnectionName : "";
269 }
270 }
271 public bool IsConnected
272 {
273 get
274 {
275 return m_connected;
276 }
277 }
278
279 public int Connect(string Connection)
280 {
281 int temp = 0;
282 uint INTERNET_AUTO_DIAL_UNATTENDED = 2;
283 int retVal = RAS.InternetDial(IntPtr.Zero, Connection, INTERNET_AUTO_DIAL_UNATTENDED, ref temp, 0);
284 return retVal;
285
286 //取消:668
287 }
288 public void Disconnect()
289 {
290 RAS.RasHangUp(m_ConnectedRasHandle);
291 }
292 }
293
294 }
295
2 using System;
3 using System.Runtime.InteropServices;
4 using System.Collections.Generic;
5 using System.Text;
6
7
8 namespace RAD
9 {
10
11 ///调用方法:
12 ///RASDisplay ras = new RASDisplay();
13 ///ras.Disconnect();//断开连接
14 ///ras.Connect("ADSL");//重新拨号
15 public struct RASCONN
16 {
17 public int dwSize;
18 public IntPtr hrasconn;
19 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 257)]
20 public string szEntryName;
21 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 17)]
22 public string szDeviceType;
23 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 129)]
24 public string szDeviceName;
25 }
26
27 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
28 public struct RasStats
29 {
30 public int dwSize;
31 public int dwBytesXmited;
32 public int dwBytesRcved;
33 public int dwFramesXmited;
34 public int dwFramesRcved;
35 public int dwCrcErr;
36 public int dwTimeoutErr;
37 public int dwAlignmentErr;
38 public int dwHardwareOverrunErr;
39 public int dwFramingErr;
40 public int dwBufferOverrunErr;
41 public int dwCompressionRatioIn;
42 public int dwCompressionRatioOut;
43 public int dwBps;
44 public int dwConnectionDuration;
45 }
46
47 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
48 public struct RasEntryName
49 {
50 public int dwSize;
51 //[MarshalAs(UnmanagedType.ByValTStr,SizeConst=(int)RasFieldSizeConstants.RAS_MaxEntryName + 1)]
52 public string szEntryName;
53 //#if WINVER5
54 // public int dwFlags;
55 // [MarshalAs(UnmanagedType.ByValTStr,SizeConst=260+1)]
56 // public string szPhonebookPath;
57 //#endif
58 }
59 public class RAS
60 {
61 [DllImport("Rasapi32.dll", EntryPoint = "RasEnumConnectionsA",
62 SetLastError = true)]
63
64 internal static extern int RasEnumConnections
65 (
66 ref RASCONN lprasconn, // buffer to receive connections data
67 ref int lpcb, // size in bytes of buffer
68 ref int lpcConnections // number of connections written to buffer
69 );
70
71
72 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
73 internal static extern uint RasGetConnectionStatistics(
74 IntPtr hRasConn, // handle to the connection
75 [In, Out]RasStats lpStatistics // buffer to receive statistics
76 );
77 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
78 public extern static uint RasHangUp(
79 IntPtr hrasconn // handle to the RAS connection to hang up
80 );
81
82 [DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
83 public extern static uint RasEnumEntries(
84 string reserved, // reserved, must be NULL
85 string lpszPhonebook, // pointer to full path and
86 // file name of phone-book file
87 [In, Out]RasEntryName[] lprasentryname, // buffer to receive
88 // phone-book entries
89 ref int lpcb, // size in bytes of buffer
90 out int lpcEntries // number of entries written
91 // to buffer
92 );
93
94 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
95 public extern static int InternetDial(
96 IntPtr hwnd,
97 [In]string lpszConnectoid,
98 uint dwFlags,
99 ref int lpdwConnection,
100 uint dwReserved
101 );
102
103 public RAS()
104 {
105 }
106 }
107 public enum DEL_CACHE_TYPE //要删除的类型。
108 {
109 File,//表示internet临时文件
110 Cookie //表示Cookie
111 }
112
113 public class RASDisplay
114 {
115 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
116 public static extern bool DeleteUrlCacheEntry(
117 DEL_CACHE_TYPE type
118 );
119 private string m_duration;
120 private string m_ConnectionName;
121 private string[] m_ConnectionNames;
122 private double m_TX;
123 private double m_RX;
124 private bool m_connected;
125 private IntPtr m_ConnectedRasHandle;
126
127 RasStats status = new RasStats();
128 public RASDisplay()
129 {
130 m_connected = true;
131
132 RAS lpras = new RAS();
133 RASCONN lprasConn = new RASCONN();
134
135 lprasConn.dwSize = Marshal.SizeOf(typeof(RASCONN));
136 lprasConn.hrasconn = IntPtr.Zero;
137
138 int lpcb = 0;
139 int lpcConnections = 0;
140 int nRet = 0;
141 lpcb = Marshal.SizeOf(typeof(RASCONN));
142
143 nRet = RAS.RasEnumConnections(ref lprasConn, ref lpcb, ref
144 lpcConnections);
145
146 if (nRet != 0)
147 {
148 m_connected = false;
149 return;
150
151 }
152
153 if (lpcConnections > 0)
154 {
155 //for (int i = 0; i < lpcConnections; i++)
156
157 //{
158 RasStats stats = new RasStats();
159
160 m_ConnectedRasHandle = lprasConn.hrasconn;
161 try
162 {
163 RAS.RasGetConnectionStatistics(lprasConn.hrasconn, stats);
164 }
165 catch (System.Exception ex)
166 {
167 throw ex;
168 }
169 finally
170 {
171
172
173 m_ConnectionName = lprasConn.szEntryName;
174
175 int Hours = 0;
176 int Minutes = 0;
177 int Seconds = 0;
178
179 Hours = ((stats.dwConnectionDuration / 1000) / 3600);
180 Minutes = ((stats.dwConnectionDuration / 1000) / 60) - (Hours * 60);
181 Seconds = ((stats.dwConnectionDuration / 1000)) - (Minutes * 60) - (Hours * 3600);
182
183
184 m_duration = Hours + " hours " + Minutes + " minutes " + Seconds + " secs";
185 m_TX = stats.dwBytesXmited;
186 m_RX = stats.dwBytesRcved;
187 //}
188 }
189 }
190 else
191 {
192 m_connected = false;
193 }
194
195
196 int lpNames = 1;
197 int entryNameSize = 0;
198 int lpSize = 0;
199 RasEntryName[] names = null;
200
201 entryNameSize = Marshal.SizeOf(typeof(RasEntryName));
202 lpSize = lpNames * entryNameSize;
203
204 names = new RasEntryName[lpNames];
205 names[0].dwSize = entryNameSize;
206
207 uint retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);
208
209 //if we have more than one connection, we need to do it again
210 if (lpNames > 1)
211 {
212 names = new RasEntryName[lpNames];
213 for (int i = 0; i < names.Length; i++)
214 {
215 names[i].dwSize = entryNameSize;
216 }
217
218 retval = RAS.RasEnumEntries(null, null, names, ref lpSize, out lpNames);
219
220 }
221 m_ConnectionNames = new string[names.Length];
222
223
224 if (lpNames > 0)
225 {
226 for (int i = 0; i < names.Length; i++)
227 {
228 m_ConnectionNames[i] = names[i].szEntryName;
229 }
230 }
231 }
232
233 public string Duration
234 {
235 get
236 {
237 return m_connected ? m_duration : "";
238 }
239 }
240
241 public string[] Connections
242 {
243 get
244 {
245 return m_ConnectionNames;
246 }
247 }
248
249 public double BytesTransmitted
250 {
251 get
252 {
253 return m_connected ? m_TX : 0;
254 }
255 }
256 public double BytesReceived
257 {
258 get
259 {
260 return m_connected ? m_RX : 0;
261
262 }
263 }
264 public string ConnectionName
265 {
266 get
267 {
268 return m_connected ? m_ConnectionName : "";
269 }
270 }
271 public bool IsConnected
272 {
273 get
274 {
275 return m_connected;
276 }
277 }
278
279 public int Connect(string Connection)
280 {
281 int temp = 0;
282 uint INTERNET_AUTO_DIAL_UNATTENDED = 2;
283 int retVal = RAS.InternetDial(IntPtr.Zero, Connection, INTERNET_AUTO_DIAL_UNATTENDED, ref temp, 0);
284 return retVal;
285
286 //取消:668
287 }
288 public void Disconnect()
289 {
290 RAS.RasHangUp(m_ConnectedRasHandle);
291 }
292 }
293
294 }
295
2、固定IP接入,代理方式。刚开始有两个方法:a、发送http请求时封装代理IP;b、通过修改注册表方式修改本机IE浏览器代理地址,然后投票。后经测试b方法耗时时间长,而且不能多线程处理最后放弃采用a方法。
a方法中采用异步调用方法,在最开始使用的是等待返回,后发现因网络延时的问题等待返回会造成程序假死状态,改为异步调用完成后回调方法。但这种方法无法调用控件,通过msdn帮助找到如何:对 Windows 窗体控件进行线程安全调用,控件多线程处理方法有三种,一种非线程安全方式,一种对 Windows 窗体控件进行线程安全调用,一种BackgroundWorker 进行的线程安全调调用。
使用异步调用完成后回调方法
声明异步调用方法和委托
//声明异步调用委托
private delegate string NewTaskDelegate(string voteAdd, string proxyAdd);
//声明委托调用方法
private static string vote(string voteAdd, string proxyAdd)
{
string html = null;
string url = voteAdd;
try
{
WebRequest req = WebRequest.Create(url);
// Create a new Uri object.
if (proxyAdd != null || proxyAdd != "")
{
Uri newUri = new Uri("http://" + proxyAdd);
WebProxy myProxy = new WebProxy();
myProxy.Address = newUri;
req.Proxy = myProxy;
}
WebResponse res = req.GetResponse();
Stream receiveStream = res.GetResponseStream();
Encoding encode = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(receiveStream, encode);
char[] readbuffer = new char[256];
int n = sr.Read(readbuffer, 0, 256);
while (n > 0)
{
string str = new string(readbuffer, 0, n);
html += str;
n = sr.Read(readbuffer, 0, 256);
}
}
catch (System.Exception ex)
{
html = ex.Message;
}
return proxyAdd + "(^_^)\r\n" + html;
}
private delegate string NewTaskDelegate(string voteAdd, string proxyAdd);
//声明委托调用方法
private static string vote(string voteAdd, string proxyAdd)
{
string html = null;
string url = voteAdd;
try
{
WebRequest req = WebRequest.Create(url);
// Create a new Uri object.
if (proxyAdd != null || proxyAdd != "")
{
Uri newUri = new Uri("http://" + proxyAdd);
WebProxy myProxy = new WebProxy();
myProxy.Address = newUri;
req.Proxy = myProxy;
}
WebResponse res = req.GetResponse();
Stream receiveStream = res.GetResponseStream();
Encoding encode = Encoding.GetEncoding("gb2312");
StreamReader sr = new StreamReader(receiveStream, encode);
char[] readbuffer = new char[256];
int n = sr.Read(readbuffer, 0, 256);
while (n > 0)
{
string str = new string(readbuffer, 0, n);
html += str;
n = sr.Read(readbuffer, 0, 256);
}
}
catch (System.Exception ex)
{
html = ex.Message;
}
return proxyAdd + "(^_^)\r\n" + html;
}
声明回调函数
private void updateWindow(IAsyncResult iar)
{
NewTaskDelegate task = (NewTaskDelegate)iar.AsyncState;
string result = task.EndInvoke(iar);
Console.WriteLine(result);
Interlocked.Decrement(ref requestCounter);
this.SetText(result);
this.SetStep();
if (requestCounter == 0)
{
this.SetButten(true);
}
}
{
NewTaskDelegate task = (NewTaskDelegate)iar.AsyncState;
string result = task.EndInvoke(iar);
Console.WriteLine(result);
Interlocked.Decrement(ref requestCounter);
this.SetText(result);
this.SetStep();
if (requestCounter == 0)
{
this.SetButten(true);
}
}
异步调用完成回调语句
1 NewTaskDelegate task = vote;
2
3 IAsyncResult asyncResult;
4
5 Interlocked.Increment(ref requestCounter);
6 AsyncCallback cb = new AsyncCallback(updateWindow);
7 asyncResult = task.BeginInvoke(this.textBox1.Text, (string)this.listBox1.SelectedItem, cb, task);
2
3 IAsyncResult asyncResult;
4
5 Interlocked.Increment(ref requestCounter);
6 AsyncCallback cb = new AsyncCallback(updateWindow);
7 asyncResult = task.BeginInvoke(this.textBox1.Text, (string)this.listBox1.SelectedItem, cb, task);
Windows 窗体控件进行线程安全调用声明
delegate void SetStepCallback();
private void SetStep()
{
if (this.progressBar1.InvokeRequired)
{
SetStepCallback c = new SetStepCallback(SetStep);
this.Invoke(c) ;
}
else
{
this.progressBar1.PerformStep();
}
}
private void SetStep()
{
if (this.progressBar1.InvokeRequired)
{
SetStepCallback c = new SetStepCallback(SetStep);
this.Invoke(c) ;
}
else
{
this.progressBar1.PerformStep();
}
}
异步调用控件语句
1 this.SetStep();