四六级批量查询系统(C#源码)

    今天我经过多方努力终于做出了”四六级查询系统“了,目前可以查整个学校,对那些忘记准考证号码的人很有帮助,其实具体的说他可以批量查询很大范围的成绩,昨晚虽然没有研究出来,但是今天99宿舍网站更改了验证方法,就算昨晚研究出结果今天也没用了。

---------------------------------

好了,好了,我的废话也说完了,现在开始进入正题,下面是一些关键的代码

昨天提交的代码是id=准考证号码&vc=novcversion今天要提交的是id=准考证&sid=cookie&vc=验证码,所以要有3方面的代码一个是判断验证码的,一个是活的COOKIE的,最后当然需要POST的代码啦!

判断验证码的也就是最难的,下面是判断验证码获取和COOKIE....关键代码如下

判断验证码和获取COOKIE 
1 privatestring getcode()
2 {
3 UnCodebase codebase =new UnCodebase(this.CodeDisCern());
4 string str ="";
5 codebase.GrayByPixels();
6 codebase.ClearPicBorder(1);
7 codebase.CleanNoise(0xce, 0xce, 0xce);
8 codebase.CleanStand(0xff, 0xff, 0xff);
9 Bitmap[] splitPics = codebase.GetSplitPics();
10 for (int i =0; i <4; i++)
11 {
12 string singleBmpCode = codebase.GetSingleBmpCode(splitPics[i], 0x80);
13 for (int j =0; j <10; j++)
14 {
15 if (this.getSemblanceBy2words(singleBmpCode, this.num[j]) >0.9)
16 {
17 str = str + j.ToString();
18 break;
19 }
20 }
21 }
22 return str;
23 }
24
25
26
27 publicfloat getSemblanceBy2words(string word1, string word2)
28 {
29 float length = word1.Length;
30 float num2 = 0f;
31 for (int i =0; i < length; i++)
32 {
33 if (word1[i] != word2[i])
34 {
35 num2++;
36 }
37 }
38 return ((length - num2) / length);
39 }
40
41 private Bitmap CodeDisCern()
42 {
43 //验证码
44   Bitmap img =new Bitmap(0x40, 25);
45 //创建http请求
46   HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://cet.99sushe.com/validatecode");
47 req.Method ="GET";
48 req.Timeout =10000;
49 //保存cookie
50   CookieContainer cc =new CookieContainer();
51 req.CookieContainer = cc;
52 try
53 {
54 //获取返回信息
55   HttpWebResponse res = (HttpWebResponse)req.GetResponse();
56 //验证码图片
57   Stream resStream = res.GetResponseStream();
58 res.Cookies = cc.GetCookies(req.RequestUri);
59 cooc = cc.GetCookies(req.RequestUri);
60 //显示获取到的验证码图片
61   img =new Bitmap(resStream);
62 return img;
63 }
64 catch (Exception ex)
65 {
66 //MessageBox.Show(ex.ToString());
67  return img;
68
69 }
70 }

下面是POST的方法,也是最重要的

POST方法
1 string result ="";
2 HttpWebResponse res =null;
3 string str2 =this.getcode();
4
5 string paramList ="id="+ id +"&sid="+this.cooc[0].Value.ToString() +"&vc="+ str2;
6
7 byte[] data = Encoding.ASCII.GetBytes(paramList);
8 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://cet.99sushe.com/getscore.html");
9 req.Method ="POST";
10 req.ContentType ="application/x-www-form-urlencoded;charset=gb2312";
11 req.Referer ="http://cet.99sushe.com";
12 req.ContentLength = data.Length;
13 Stream newStream = req.GetRequestStream();
14 newStream.Write(data, 0, data.Length);
15 newStream.Close();
16 res = (HttpWebResponse)req.GetResponse();
17 Stream s = res.GetResponseStream();
18 StreamReader sr =new StreamReader(s, Encoding.GetEncoding("GB2312"));
19 result = sr.ReadToEnd();
20

按照上面两个代码,基本上来个循环就能查整个学校的成绩了,下面是循环(写的不是很好)

循环方法
1 for (int i = start; i <= end; i++)
2 for (int n =1; n <31; n++)
3 {
4 if (i <10)
5 {
6 kc ="00"+ i.ToString();
7 }
8 if (i <100&& i >=10)
9 {
10 kc ="0"+ i.ToString();
11 }
12 if (n <10)
13 {
14 zh ="0"+ n.ToString();
15 }
16 else
17 {
18 zh = n.ToString();
19 }
20
21 richTextBox1.Text = richTextBox1.Text +"3510300921"+ kc + zh +"\n"+ query("3510300921"+ kc + zh) +"\n";
22 }

好了,基本介绍完毕了,总的来说我还是很惭愧的,没有真正自己研究出来......这里再次向shilin8805表示感谢!、

------------

不好意思关键代码没发全,少发了个类UncodeBase.cs,下面是完整代码!

判断验证码的类
1 using System;
2 using System.Drawing;
3 using System.Drawing.Imaging;
4 using System.Runtime.InteropServices;
5
6 namespace CET
7 {
8 internalclass UnCodebase
9 {
10 public Bitmap bmpobj;
11
12 public UnCodebase(Bitmap pic)
13 {
14 this.bmpobj =new Bitmap(pic);
15 }
16
17 public Bitmap CleanNoise(int R, int G, int B)
18 {
19 for (int i =0; i <this.bmpobj.Height; i++)
20 {
21 for (int j =0; j <this.bmpobj.Width; j++)
22 {
23 Color pixel =this.bmpobj.GetPixel(j, i);
24 if (((pixel.R.ToString() == R.ToString()) && (pixel.G.ToString() == G.ToString())) && (pixel.B.ToString() == B.ToString()))
25 {
26 this.bmpobj.SetPixel(j, i, Color.FromArgb(0xff, 0xff, 0xff));
27 }
28 }
29 }
30 returnthis.bmpobj;
31 }
32
33 public Bitmap CleanStand(int R, int G, int B)
34 {
35 int num =0;
36 Color color = Color.FromArgb(R, G, B);
37 for (int i =1; i < (this.bmpobj.Height -1); i++)
38 {
39 for (int j =1; j < (this.bmpobj.Width -1); j++)
40 {
41 if (this.bmpobj.GetPixel(j -1, i -1) != color)
42 {
43 num++;
44 }
45 if (this.bmpobj.GetPixel(j -1, i) != color)
46 {
47 num++;
48 }
49 if (this.bmpobj.GetPixel(j -1, i +1) != color)
50 {
51 num++;
52 }
53 if (this.bmpobj.GetPixel(j, i -1) != color)
54 {
55 num++;
56 }
57 if (this.bmpobj.GetPixel(j, i +1) != color)
58 {
59 num++;
60 }
61 if (this.bmpobj.GetPixel(j +1, i) != color)
62 {
63 num++;
64 }
65 if (this.bmpobj.GetPixel(j +1, i +1) != color)
66 {
67 num++;
68 }
69 if (this.bmpobj.GetPixel(j +1, i -1) != color)
70 {
71 num++;
72 }
73 if (num <2)
74 {
75 this.bmpobj.SetPixel(j, i, color);
76 }
77 num =0;
78 }
79 }
80 returnthis.bmpobj;
81 }
82
83 public Bitmap ClearPicBorder(int borderWidth)
84 {
85 for (int i =0; i <this.bmpobj.Height; i++)
86 {
87 for (int j =0; j <this.bmpobj.Width; j++)
88 {
89 if (((i < borderWidth) || (j < borderWidth)) || ((j > ((this.bmpobj.Width -1) - borderWidth)) || (i > ((this.bmpobj.Height -1) - borderWidth))))
90 {
91 this.bmpobj.SetPixel(j, i, Color.FromArgb(0xff, 0xff, 0xff));
92 }
93 }
94 }
95 returnthis.bmpobj;
96 }
97
98 privateint GetGrayNumColor(Color posClr)
99 {
100 return ((((posClr.R *0x4c8b) + (posClr.G *0x9645)) + (posClr.B *0x1d30)) >>0x10);
101 }
102
103 public Bitmap GetPicValidByValue(int dgGrayValue)
104 {
105 int width =this.bmpobj.Width;
106 int height =this.bmpobj.Height;
107 int num3 =0;
108 int num4 =0;
109 for (int i =0; i <this.bmpobj.Height; i++)
110 {
111 for (int j =0; j <this.bmpobj.Width; j++)
112 {
113 if (this.bmpobj.GetPixel(j, i).R < dgGrayValue)
114 {
115 if (width > j)
116 {
117 width = j;
118 }
119 if (height > i)
120 {
121 height = i;
122 }
123 if (num3 < j)
124 {
125 num3 = j;
126 }
127 if (num4 < i)
128 {
129 num4 = i;
130 }
131 }
132 }
133 }
134 Rectangle rect =new Rectangle(width, height, (num3 - width) +1, (num4 - height) +1);
135 this.bmpobj =this.bmpobj.Clone(rect, this.bmpobj.PixelFormat);
136 returnthis.bmpobj;
137 }
138
139 public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
140 {
141 int width = singlepic.Width;
142 int height = singlepic.Height;
143 int num3 =0;
144 int num4 =0;
145 for (int i =0; i < singlepic.Height; i++)
146 {
147 for (int j =0; j < singlepic.Width; j++)
148 {
149 if (singlepic.GetPixel(j, i).R < dgGrayValue)
150 {
151 if (width > j)
152 {
153 width = j;
154 }
155 if (height > i)
156 {
157 height = i;
158 }
159 if (num3 < j)
160 {
161 num3 = j;
162 }
163 if (num4 < i)
164 {
165 num4 = i;
166 }
167 }
168 }
169 }
170 Rectangle rect =new Rectangle(width, height, (num3 - width) +1, (num4 - height) +1);
171 return singlepic.Clone(rect, singlepic.PixelFormat);
172 }
173
174 public Bitmap GetPicValidByValue(int dgGrayValue, int CharsCount)
175 {
176 int width =this.bmpobj.Width;
177 int height =this.bmpobj.Height;
178 int num3 =0;
179 int num4 =0;
180 for (int i =0; i <this.bmpobj.Height; i++)
181 {
182 for (int j =0; j <this.bmpobj.Width; j++)
183 {
184 if (this.bmpobj.GetPixel(j, i).R < dgGrayValue)
185 {
186 if (width > j)
187 {
188 width = j;
189 }
190 if (height > i)
191 {
192 height = i;
193 }
194 if (num3 < j)
195 {
196 num3 = j;
197 }
198 if (num4 < i)
199 {
200 num4 = i;
201 }
202 }
203 }
204 }
205 int num8 = CharsCount - (((num3 - width) +1) % CharsCount);
206 if (num8 < CharsCount)
207 {
208 int num9 = num8 /2;
209 if (width > num9)
210 {
211 width -= num9;
212 }
213 if (((num3 + num8) - num9) <this.bmpobj.Width)
214 {
215 num3 = (num3 + num8) - num9;
216 }
217 }
218 Rectangle rect =new Rectangle(width, height, (num3 - width) +1, (num4 - height) +1);
219 this.bmpobj =this.bmpobj.Clone(rect, this.bmpobj.PixelFormat);
220 returnthis.bmpobj;
221 }
222
223 publicstring GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
224 {
225 string str ="";
226 for (int i =0; i < singlepic.Height; i++)
227 {
228 for (int j =0; j < singlepic.Width; j++)
229 {
230 if (singlepic.GetPixel(j, i).R < dgGrayValue)
231 {
232 str = str +"@";
233 }
234 else
235 {
236 str = str +".";
237 }
238 }
239 }
240 return str;
241 }
242
243 public Bitmap[] GetSplitPics()
244 {
245 returnnew Bitmap[] { this.bmpobj.Clone(new Rectangle(12, 5, 7, 14), this.bmpobj.PixelFormat), this.bmpobj.Clone(new Rectangle(0x17, 5, 7, 14), this.bmpobj.PixelFormat), this.bmpobj.Clone(new Rectangle(0x22, 5, 7, 14), this.bmpobj.PixelFormat), this.bmpobj.Clone(new Rectangle(0x2d, 5, 7, 14), this.bmpobj.PixelFormat) };
246 }
247
248 public Bitmap[] GetSplitPics(int RowNum, int ColNum)
249 {
250 if ((RowNum ==0) || (ColNum ==0))
251 {
252 returnnull;
253 }
254 int width =this.bmpobj.Width / RowNum;
255 int height =this.bmpobj.Height / ColNum;
256 Bitmap[] bitmapArray =new Bitmap[RowNum * ColNum];
257 for (int i =0; i < ColNum; i++)
258 {
259 for (int j =0; j < RowNum; j++)
260 {
261 Rectangle rect =new Rectangle(j * width, i * height, width, height);
262 bitmapArray[(i * RowNum) + j] =this.bmpobj.Clone(rect, this.bmpobj.PixelFormat);
263 }
264 }
265 return bitmapArray;
266 }
267
268 publicvoid GrayByLine()
269 {
270 Rectangle rect =new Rectangle(0, 0, this.bmpobj.Width, this.bmpobj.Height);
271 BitmapData bitmapdata =this.bmpobj.LockBits(rect, ImageLockMode.ReadWrite, this.bmpobj.PixelFormat);
272 IntPtr source = bitmapdata.Scan0;
273 int length =this.bmpobj.Width *this.bmpobj.Height;
274 int[] destination =newint[length];
275 Marshal.Copy(source, destination, 0, length);
276 int red =0;
277 for (int i =0; i < length; i++)
278 {
279 red =this.GetGrayNumColor(Color.FromArgb(destination[i]));
280 destination[i] = (byte)Color.FromArgb(red, red, red).ToArgb();
281 }
282 this.bmpobj.UnlockBits(bitmapdata);
283 }
284
285 public Bitmap GrayByPixels()
286 {
287 for (int i =0; i <this.bmpobj.Height; i++)
288 {
289 for (int j =0; j <this.bmpobj.Width; j++)
290 {
291 int grayNumColor =this.GetGrayNumColor(this.bmpobj.GetPixel(j, i));
292 this.bmpobj.SetPixel(j, i, Color.FromArgb(grayNumColor, grayNumColor, grayNumColor));
293 }
294 }
295 returnthis.bmpobj;
296 }
297 }
298 }
299
300

终于发完整了,有问题,在留言吧!谢谢指教!

原文地址:https://www.cnblogs.com/cracker/p/cet.html