关于校内相册图片获取
2010-02-22 14:03 cnb_mtime 阅读(644) 评论(0) 编辑 收藏 举报![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
年前一直很忙,没机会备忘,今天上班,记一下
当时是在逛校内网时看到某个相册有以前多张图片,无奈用迅雷下载的时候发现地址没规律。。。
就随便写了个小程序,无奈对winform一窍不通,只是本着解决问题的念头做的,只是记一下获取的过程,上次用网页做了个这样的程序,不过功能实在说不过去。。。。下面是代码:
1 using System;
2 using System.Drawing;
3 using System.Text;
4 using System.Windows.Forms;
5 using System.Net;
6 using System.IO;
7 using System.Text.RegularExpressions;
8 using System.Threading;
9
10 namespace WindowsApplication1
11 {
12 public partial class LoginForm : Form
13 {
14 /// <summary>
15 /// Required designer variable.
16 /// </summary>
17 private System.ComponentModel.IContainer components = null;
18 private TextBox textBox3;
19 public CookieContainer cookiecontainer = null;
20 string HtmlBody = "";
21 string bstr = "";
22 string estr = "";
23 string u_url = "";
24 string urlwebbrower = "";
25 string savepath = "c:\\";
26 Thread mythread;
27 private TextBox textBox4;
28 private Label label3;
29 private Label label4;
30 private TextBox textBox5;
31 private Button button2;
32 private FolderBrowserDialog folderBrowserDialog1;
33 private ProgressBar progressBar1;
34 private CheckBox checkBox1;
35 private NotifyIcon notifyIcon1;
36 private WebBrowser webBrowser1;
37 private ContextMenuStrip contextMenuStrip1;
38 private ToolStripMenuItem 显示ToolStripMenuItem;
39 private ToolStripMenuItem 隐藏ToolStripMenuItem;
40 private ToolStripMenuItem 退出ToolStripMenuItem;
41 private System.Windows.Forms.Timer timer1;
42 HttpWebRequest request = null;
43
44 /// <summary>
45 /// Clean up any resources being used.
46 /// </summary>
47 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
48 protected override void Dispose(bool disposing)
49 {
50 if (disposing && (components != null))
51 {
52 components.Dispose();
53 }
54 base.Dispose(disposing);
55 }
56
57 #region Windows Form Designer generated code
58
59 /// <summary>
60 /// Required method for Designer support - do not modify
61 /// the contents of this method with the code editor.
62 /// </summary>
63 private void InitializeComponent()
64 {
65 this.components = new System.ComponentModel.Container();
66 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginForm));
67 this.textBox1 = new System.Windows.Forms.TextBox();
68 this.textBox2 = new System.Windows.Forms.TextBox();
69 this.label1 = new System.Windows.Forms.Label();
70 this.label2 = new System.Windows.Forms.Label();
71 this.button1 = new System.Windows.Forms.Button();
72 this.textBox3 = new System.Windows.Forms.TextBox();
73 this.textBox4 = new System.Windows.Forms.TextBox();
74 this.label3 = new System.Windows.Forms.Label();
75 this.label4 = new System.Windows.Forms.Label();
76 this.textBox5 = new System.Windows.Forms.TextBox();
77 this.button2 = new System.Windows.Forms.Button();
78 this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
79 this.progressBar1 = new System.Windows.Forms.ProgressBar();
80 this.checkBox1 = new System.Windows.Forms.CheckBox();
81 this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
82 this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
83 this.显示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
84 this.隐藏ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
85 this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
86 this.webBrowser1 = new System.Windows.Forms.WebBrowser();
87 this.timer1 = new System.Windows.Forms.Timer(this.components);
88 this.contextMenuStrip1.SuspendLayout();
89 this.SuspendLayout();
90 //
91 // textBox1
92 //
93 this.textBox1.Location = new System.Drawing.Point(77, 16);
94 this.textBox1.Name = "textBox1";
95 this.textBox1.Size = new System.Drawing.Size(100, 23);
96 this.textBox1.TabIndex = 0;
97 this.textBox1.Text = "568470306@qq.com";
98 //
99 // textBox2
100 //
101 this.textBox2.Location = new System.Drawing.Point(77, 54);
102 this.textBox2.Name = "textBox2";
103 this.textBox2.PasswordChar = '*';
104 this.textBox2.Size = new System.Drawing.Size(100, 23);
105 this.textBox2.TabIndex = 1;
106 this.textBox2.Text = "qinqinppz";
107 //
108 // label1
109 //
110 this.label1.AutoSize = true;
111 this.label1.Location = new System.Drawing.Point(13, 24);
112 this.label1.Name = "label1";
113 this.label1.Size = new System.Drawing.Size(56, 17);
114 this.label1.TabIndex = 2;
115 this.label1.Text = "用户名:";
116 //
117 // label2
118 //
119 this.label2.AutoSize = true;
120 this.label2.Location = new System.Drawing.Point(13, 58);
121 this.label2.Name = "label2";
122 this.label2.Size = new System.Drawing.Size(56, 17);
123 this.label2.TabIndex = 3;
124 this.label2.Text = "密 码:";
125 //
126 // button1
127 //
128 this.button1.Location = new System.Drawing.Point(214, 53);
129 this.button1.Name = "button1";
130 this.button1.Size = new System.Drawing.Size(75, 23);
131 this.button1.TabIndex = 4;
132 this.button1.Text = "确定";
133 this.button1.UseVisualStyleBackColor = true;
134 this.button1.Click += new System.EventHandler(this.button1_Click);
135 //
136 // textBox3
137 //
138 this.textBox3.Location = new System.Drawing.Point(12, 123);
139 this.textBox3.Multiline = true;
140 this.textBox3.Name = "textBox3";
141 this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
142 this.textBox3.Size = new System.Drawing.Size(830, 236);
143 this.textBox3.TabIndex = 6;
144 this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
145 //
146 // textBox4
147 //
148 this.textBox4.Location = new System.Drawing.Point(76, 90);
149 this.textBox4.Name = "textBox4";
150 this.textBox4.Size = new System.Drawing.Size(681, 23);
151 this.textBox4.TabIndex = 8;
152 this.textBox4.Text = "http://photo.renren.com/getalbum.do?id=347985504&owner=249664496&ref=sharenewsfee" +
153 "d";
154 //
155 // label3
156 //
157 this.label3.AutoSize = true;
158 this.label3.Location = new System.Drawing.Point(13, 93);
159 this.label3.Name = "label3";
160 this.label3.Size = new System.Drawing.Size(56, 17);
161 this.label3.TabIndex = 5;
162 this.label3.Text = "地 址:";
163 this.label3.Click += new System.EventHandler(this.label3_Click);
164 //
165 // label4
166 //
167 this.label4.AutoSize = true;
168 this.label4.Location = new System.Drawing.Point(213, 21);
169 this.label4.Name = "label4";
170 this.label4.Size = new System.Drawing.Size(20, 17);
171 this.label4.TabIndex = 9;
172 this.label4.Text = " ";
173 //
174 // textBox5
175 //
176 this.textBox5.Location = new System.Drawing.Point(307, 15);
177 this.textBox5.Multiline = true;
178 this.textBox5.Name = "textBox5";
179 this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
180 this.textBox5.Size = new System.Drawing.Size(450, 60);
181 this.textBox5.TabIndex = 10;
182 //
183 // button2
184 //
185 this.button2.Location = new System.Drawing.Point(767, 89);
186 this.button2.Name = "button2";
187 this.button2.Size = new System.Drawing.Size(75, 23);
188 this.button2.TabIndex = 11;
189 this.button2.Text = "OK";
190 this.button2.UseVisualStyleBackColor = true;
191 this.button2.Click += new System.EventHandler(this.button2_Click);
192 //
193 // progressBar1
194 //
195 this.progressBar1.Location = new System.Drawing.Point(12, 562);
196 this.progressBar1.Name = "progressBar1";
197 this.progressBar1.Size = new System.Drawing.Size(830, 20);
198 this.progressBar1.Step = 1;
199 this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
200 this.progressBar1.TabIndex = 12;
201 //
202 // checkBox1
203 //
204 this.checkBox1.AutoSize = true;
205 this.checkBox1.Location = new System.Drawing.Point(767, 62);
206 this.checkBox1.Name = "checkBox1";
207 this.checkBox1.Size = new System.Drawing.Size(75, 21);
208 this.checkBox1.TabIndex = 13;
209 this.checkBox1.Text = "保存评论";
210 this.checkBox1.UseVisualStyleBackColor = true;
211 //
212 // notifyIcon1
213 //
214 this.notifyIcon1.BalloonTipText = "最小化";
215 this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
216 this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
217 this.notifyIcon1.Text = "最小化";
218 this.notifyIcon1.Visible = true;
219 this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
220 //
221 // contextMenuStrip1
222 //
223 this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
224 this.显示ToolStripMenuItem,
225 this.隐藏ToolStripMenuItem,
226 this.退出ToolStripMenuItem});
227 this.contextMenuStrip1.Name = "contextMenuStrip1";
228 this.contextMenuStrip1.Size = new System.Drawing.Size(101, 70);
229 //
230 // 显示ToolStripMenuItem
231 //
232 this.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem";
233 this.显示ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
234 this.显示ToolStripMenuItem.Text = "显示";
235 this.显示ToolStripMenuItem.Click += new System.EventHandler(this.显示ToolStripMenuItem_Click);
236 //
237 // 隐藏ToolStripMenuItem
238 //
239 this.隐藏ToolStripMenuItem.Name = "隐藏ToolStripMenuItem";
240 this.隐藏ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
241 this.隐藏ToolStripMenuItem.Text = "隐藏";
242 this.隐藏ToolStripMenuItem.Click += new System.EventHandler(this.隐藏ToolStripMenuItem_Click);
243 //
244 // 退出ToolStripMenuItem
245 //
246 this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
247 this.退出ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
248 this.退出ToolStripMenuItem.Text = "退出";
249 this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
250 //
251 // webBrowser1
252 //
253 this.webBrowser1.Location = new System.Drawing.Point(12, 365);
254 this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
255 this.webBrowser1.Name = "webBrowser1";
256 this.webBrowser1.Size = new System.Drawing.Size(830, 191);
257 this.webBrowser1.TabIndex = 7;
258 //
259 // timer1
260 //
261 this.timer1.Enabled = true;
262 this.timer1.Interval = 1000;
263 this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
264 //
265 // LoginForm
266 //
267 this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
268 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
269 this.BackColor = System.Drawing.SystemColors.Control;
270 this.ClientSize = new System.Drawing.Size(851, 588);
271 this.Controls.Add(this.checkBox1);
272 this.Controls.Add(this.progressBar1);
273 this.Controls.Add(this.button2);
274 this.Controls.Add(this.textBox5);
275 this.Controls.Add(this.label4);
276 this.Controls.Add(this.textBox4);
277 this.Controls.Add(this.webBrowser1);
278 this.Controls.Add(this.textBox3);
279 this.Controls.Add(this.label3);
280 this.Controls.Add(this.button1);
281 this.Controls.Add(this.label2);
282 this.Controls.Add(this.label1);
283 this.Controls.Add(this.textBox2);
284 this.Controls.Add(this.textBox1);
285 this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
286 this.ForeColor = System.Drawing.SystemColors.ControlText;
287 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
288 this.Name = "LoginForm";
289 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
290 this.Text = "校内……";
291 this.Load += new System.EventHandler(this.LoginForm_Load);
292 this.Resize += new System.EventHandler(this.LoginForm_Resize);
293
294 this.contextMenuStrip1.ResumeLayout(false);
295 this.ResumeLayout(false);
296 this.PerformLayout();
297
298 }
299
300 #endregion
301
302 private System.Windows.Forms.TextBox textBox1;
303 private System.Windows.Forms.TextBox textBox2;
304 private System.Windows.Forms.Label label1;
305 private System.Windows.Forms.Label label2;
306 private System.Windows.Forms.Button button1;
307
308 public LoginForm()
309 {
310 InitializeComponent();
311 this.button2.Enabled = false;
312 int count = this.Controls.Count * 2 + 2;
313 float[] factor = new float[count];
314 int i = 0;
315 factor[i++] = Size.Width;
316 factor[i++] = Size.Height;
317 foreach (Control ctrl in this.Controls)
318 {
319 factor[i++] = ctrl.Location.X / (float)Size.Width;
320 factor[i++] = ctrl.Location.Y / (float)Size.Height;
321 ctrl.Tag = ctrl.Size;
322 }
323 Tag = factor;
324
325 }
326 private void LoginForm_Resize(object sender, EventArgs e)
327 {
328 float[] scale = (float[])Tag;
329 int i = 2;
330
331 foreach (Control ctrl in this.Controls)
332 {
333 ctrl.Left = (int)(Size.Width * scale[i++]);
334 ctrl.Top = (int)(Size.Height * scale[i++]);
335 ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
336 ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
337 //每次使用的都是最初始的控件大小,保证准确无误。
338 }
339 }
340 /// <summary>
341 /// 应用程序的主入口点。
342 /// </summary>
343 //[STAThread]
344 //static void Main()
345 //{
346 // Application.Run(new LoginForm());
347 //}
348 //获取相册详细信息
349 private void getPhotosinf(string url)
350 {
351 this.folderBrowserDialog1.Description = "请选择保存输出图片的文件夹";
352 this.folderBrowserDialog1.ShowNewFolderButton = true;
353 this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory;
354 DialogResult result_path = folderBrowserDialog1.ShowDialog();
355 u_url = url;
356 if (result_path == DialogResult.OK)
357 {
358 notifyIcon1.Text = "正在下载,请稍候...";
359 string folderName = folderBrowserDialog1.SelectedPath;
360 if (folderName != "")
361 {
362 savepath = folderName;
363 this.Text = savepath;
364 }
365 //mythread = new Thread(new ThreadStart(DoWork));
366 mythread = new Thread(new ThreadStart(getData));
367 mythread.Start();
368 //getData();
369 }
370 }
371
372 public delegate void myDelegate(string name);
373 //private void DoWork()
374 //{
375 // myDelegate md = new myDelegate(getData);
376 // this.BeginInvoke(md,"11");
377 //}
378 //程序核心部分
379 private void getData()
380 {
381 string url = u_url;
382 string code = getHtmlCode(url);
383 string friendname = "";
384 string photosname = "";
385 string photoscount = "";
386 friendname = getinfo(code, "<input type=\"hidden\" id=\"fromname\" name=\"fromname\" id=\"fromname\" value=\"", "\" />", "([\\s\\S]*?)");
387 photosname = getinfo(code, "<input type=\"hidden\" id=\"title\" name=\"title\" value=\"", "\" />", "([\\s\\S]*?)");
388 photoscount = getinfo(code, "<div id=\"summary\" name=\"summary\" style=\"display:none\">", "</div>", "([\\s\\S]*?)");
389 string phoinf = "相册名称:【" + photosname + "】;\n照片数量:【" + photoscount + "】; \n相册所属ID:【" + friendname + "】";
390 this.textBox5.Text = "";
391 this.textBox5.AppendText(phoinf);
392
393 MatchCollection matchs = null;
394 this.textBox3.Text = "";
395 int num = 0;
396 for (int i = 0; i < (int.Parse(photoscount) + 16 - 1) / 16; i++)
397 {
398 if (url.IndexOf("?") != -1)
399 {
400 code = getHtmlCode(url + "&curpage=" + i);
401 }
402 else
403 {
404 code = getHtmlCode(url + "?curpage=" + i);
405 }
406 matchs = getinfos(code, "<span class=\"img\">\\n<a href=\"", "\">", "([\\s\\S]*?)");
407 this.textBox3.AppendText("正在获取第 " + (i + 1) + " 页\n");
408
409 foreach (Match m in matchs)
410 {
411 num++;
412 code = getHtmlCode(m.Groups[1].Value);
413 string imgurl = getinfo(getinfo(code, "var photo = {", "]};", "([\\s\\S]*?)").Replace(@"\/", "/"), "largeurl\":\"", "\",\"summary\"", "([\\s\\S]*?\\.[\\w]{3,5})");
414 string jutimingzi = getFilenameStr(getinfo(code, "<input type=\"hidden\" id=\"title\" name=\"title\" value=\"", "\" />", "([\\s\\S]*?)"));
415 this.textBox3.AppendText(num + "-->>\t" + imgurl + "\n");
416 string[] filenames = imgurl.Split('/');
417 string filename = savepath + "\\" + num + "_" + (i + 1) + "_" + jutimingzi + filenames[filenames.Length - 1];
418 if (this.checkBox1.Checked)
419 {
420 string strcontent = getUnicode(getinfo(code, "var photo = {", "]};", "([\\s\\S]*?)").Replace(@"\/", "/"));
421 string strcontentfilename = filename.Split('.')[0] + ".txt";
422 saveFileTxt(strcontentfilename, strcontent);
423 }
424 DownloadFile(imgurl, filename);
425 urlwebbrower += "<img src=\"" + imgurl + "\" />";
426
427 }
428 }
429 this.webBrowser1.DocumentText = urlwebbrower;
430 notifyIcon1.Text = "单击显示隐藏窗口";
431 }
432 //获取用户真实姓名
433 private string realName(string htmlbody)
434 {
435 bstr = "<title>";
436 estr = "</title>";
437 string _regex = bstr+"([\\s\\S]*?)"+estr;
438 Match titlematch = Regex.Match(htmlbody, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
439 bstr = "";
440 estr = "";
441 return titlematch.Groups[1].Value.Trim().Split('-')[titlematch.Groups[1].Value.Trim().Split('-').Length-1];
442 }
443 //根据开始结束字符串得到匹配项
444 private string getinfo(string str0, string str1, string str2, string regex)
445 {
446 bstr = str1;
447 estr = str2;
448 string _regex = bstr + regex + estr;
449 Match match = Regex.Match(str0, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
450 bstr = "";
451 estr = "";
452 return match.Groups[1].Value;
453 }
454 //根据开始结束字符串得到匹配项(集合)
455 private MatchCollection getinfos(string str0, string str1, string str2, string regex)
456 {
457 bstr = str1;
458 estr = str2;
459 string _regex = bstr + regex + estr;
460 //str0 = str0.Replace(@"\n", "");
461 MatchCollection matchs = Regex.Matches(str0, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
462 bstr = "";
463 estr = "";
464 return matchs;
465 }
466 //构造表单,模拟登录
467 private string activeLogin(string username,string password,string indexurl)
468 {
469 cookiecontainer = null;
470 cookiecontainer= new CookieContainer();
471 request = (HttpWebRequest)WebRequest.Create(indexurl);
472 string actiondata = "email=" + username + "&password=" + password;
473 byte[] b = System.Text.Encoding.UTF8.GetBytes(actiondata);
474 Encoding code = Encoding.GetEncoding("UTF-8");
475 request.CookieContainer = cookiecontainer;
476
477 request.Method = "POST";
478 request.KeepAlive = false;
479 request.ContentType = "application/x-www-form-urlencoded";
480 request.CookieContainer = cookiecontainer;
481 // 提交请求数据
482 System.IO.Stream outputStream = request.GetRequestStream();
483 outputStream.Write(b, 0, b.Length);
484 outputStream.Close();
485 // 接收返回的页面
486 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
487 StreamReader sr = new StreamReader(response.GetResponseStream(),code);
488 string result = sr.ReadToEnd();
489 string rl = "";
490 while((rl = sr.ReadLine())!=null )
491 {
492 result += rl;
493 }
494 foreach (Cookie cookie in response.Cookies)
495 cookiecontainer.Add(cookie);
496 this.webBrowser1.Show();
497 //this.webBrowser1.Url = response.ResponseUri;
498 //this.webBrowser1.DocumentText = result;
499 return "【" + cookiecontainer .Count+ "】" + result;
500 }
501
502 /// <summary>
503 ///获取校内的登录页地址
504 /// </summary>
505 /// <param name="indexUrl"></param>
506 /// <returns></returns>
507 private string actionUrl(string indexUrl)
508 {
509 string index = indexUrl;
510 Encoding code = Encoding.GetEncoding("UTF-8");
511 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.renren.com");
512 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
513 StreamReader sr = new StreamReader(response.GetResponseStream(), code);
514 string str = sr.ReadLine();
515
516 string hyperlink1= "<form method=\"post\" id=\"loginForm\" class=\"login-form\" action=\"http://www.renren.com/PLogin.do\" >";
517 Regex reg = new Regex("http(s)?://([\\w]+\\.)?([\\w]+\\.)?([\\w]+)?/([\\w]+\\.)?([\\w]+)?");
518 Match m=reg.Match(hyperlink1);
519 string a=m.Value;
520 return a;
521 }
522 //获取请求的地址的源码
523 public string getHtmlCode(String url)
524 {
525 request = (HttpWebRequest)WebRequest.Create(url);
526 request.CookieContainer = cookiecontainer;
527 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
528 Stream stream = response.GetResponseStream();
529 StreamReader sr = new StreamReader(stream, Encoding.UTF8);
530 String text = sr.ReadToEnd();
531 return text;
532 }
533 //将评论保存为文本文件
534 private void saveFileTxt(string filename,string strcontent)
535 {
536 //if (!File.Exists(filename))
537 //{
538 // File.Create(filename);
539 //}
540 //处理评论部分的UNICODE编码
541
542 string outstr = "";
543 MatchCollection matchs = getinfos(strcontent, "\"body\":\"<span>", "</span>\",\"author\"", "([\\s\\S]*?)");
544 //将评论正序输出
545 //foreach(Match m in matchs)
546 //{
547 // string _str = m.Groups[1].Value.Replace(@"\r\n", "\r\n") + "\r\n";
548 // _str = Regex.Replace(_str, @"</?[a-z]+[\s\S]*?/?>","");
549 // outstr += _str;
550 //}
551
552 //将评论倒叙输出
553 for (int i = matchs.Count-1; i >=0;i-- )
554 {
555 string _str = matchs[i].Groups[1].Value.Replace(@"\r\n", "\r\n") + "\r\n";
556 _str = Regex.Replace(_str, @"</?[a-z]+[\s\S]*?/?>", "");
557 outstr += _str;
558 }
559 StreamWriter sw = new StreamWriter(filename);
560 sw.Write(outstr);
561 sw.Flush();
562 sw.Close();
563 sw.Dispose();
564 }
565 //匹配UNICODE字符串
566 private string getUnicode(string unicodestr)
567 {
568 string outstr = "";
569 for (int i = 0; i < unicodestr.Length-7;)
570 {
571 string sss = unicodestr.Substring(i, 6);
572 if (Regex.IsMatch(unicodestr.Substring(i, 6), @"\\u[a-z0-9]{4}"))
573 {
574 outstr += unicodeToasc(unicodestr.Substring(i, 6));
575 i = i + 6;
576 }
577 else
578 {
579 outstr += unicodestr.Substring(i, 1);
580 i = i + 1;
581 }
582 }
583 return outstr;
584 }
585 //过滤文件名中的非法字符串,返回文件名中的前十个字符
586 private string getFilenameStr(string _filename)
587 {
588 string _str = _filename;
589 if (_str.Length >= 20)
590 {
591 _str = _str.Substring(0, 20);
592 }
593 _str = _str.Replace("\\", string.Empty);
594 _str = _str.Replace("/", string.Empty);
595 _str = _str.Replace(":", string.Empty);
596 _str = _str.Replace("*", string.Empty);
597 _str = _str.Replace("?", string.Empty);
598 _str = _str.Replace("\"", string.Empty);
599 _str = _str.Replace("<", string.Empty);
600 _str = _str.Replace(">", string.Empty);
601 _str = _str.Replace("|", string.Empty);
602 _str = _str.Replace("\r\n", "");
603 _str = _str.Replace("\n", "");
604 _str = _str.Replace("\r", "");
605 //int ii = _str.IndexOf("\r\n");
606 _str = _str.Replace(" ", string.Empty); //前面的替换会产生空格,最后将其一并替换掉
607 return _str;
608 }
609 //UNICODE字符转为中文
610 private string unicodeToasc(string unicodestr)
611 {
612 unicodestr = unicodestr.Replace("\\u","\\");
613 string[] value1 = unicodestr.Split('\\');
614 string tempValue1 = "";
615 string tempValue2 = "";
616
617 string strOut = "";
618 foreach (string temp in value1)
619 {
620 if (temp.Length == 0)
621 {
622 continue;
623 }
624 try
625 {
626 tempValue1 = temp;
627 tempValue2 = "";
628 if (tempValue1.Length > 4)
629 {
630 tempValue1 = temp.Substring(0, 4);
631 tempValue2 = temp.Substring(4, temp.Length - 4);
632 }
633 int intASCII = Convert.ToInt32(tempValue1, 16);
634 strOut += ((char)intASCII).ToString() + tempValue2;
635 }
636 catch
637 {
638 strOut += temp;
639 }
640 }
641 return strOut;
642 }
643
644 /// <summary>
645 /// 下载文件
646 /// </summary>
647 /// <param name="URL">下载文件地址</param>
648 /// <param name="Filename">下载后的存放地址</param>
649 /// <param name="Prog">用于显示的进度条</param>
650 public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
651 {
652 try
653 {
654 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
655 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
656 long totalBytes = myrp.ContentLength;
657 if (prog != null)
658 {
659 prog.Maximum = (int)totalBytes;
660 }
661 System.IO.Stream st = myrp.GetResponseStream();
662 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
663 long totalDownloadedByte = 0;
664 byte[] by = new byte[1024];
665 int osize = st.Read(by, 0, (int)by.Length);
666 while (osize > 0)
667 {
668 totalDownloadedByte = osize + totalDownloadedByte;
669 so.Write(by, 0, osize);
670
671 if (prog != null)
672 {
673 prog.Value = (int)totalDownloadedByte;
674 }
675 osize = st.Read(by, 0, (int)by.Length);
676 }
677 so.Flush();
678 so.Close();
679 st.Close();
680 }
681 catch (System.Exception)
682 {
683 throw;
684 }
685 }
686 /// <summary>
687 /// 下载文件
688 /// </summary>
689 /// <param name="URL">下载文件地址</param>
690 /// <param name="Filename">下载后的存放地址</param>
691 public void DownloadFile(string URL, string filename)
692 {
693 DownloadFile(URL, filename,this.progressBar1);
694 }
695
696 private void label3_Click(object sender, EventArgs e)
697 {
698
699 }
700 private void textBox3_TextChanged(object sender, EventArgs e)
701 {
702
703 }
704
705 private void LoginForm_Load(object sender, EventArgs e)
706 {
707 notifyIcon1.Icon = new Icon("reload.ico");
708 notifyIcon1.Visible = true;
709 notifyIcon1.Text = "单击显示隐藏窗口";
710 }
711 private void button1_Click(object sender, EventArgs e)
712 {
713 string username = textBox1.Text;
714 string password = textBox2.Text;
715 HtmlBody = activeLogin(username, password, actionUrl(""));
716 this.textBox3.Text = HtmlBody;
717 if (cookiecontainer.Count == 9)
718 {
719 this.button1.Enabled = false;
720 this.button2.Enabled = true;
721 }
722 this.label4.Text = realName(HtmlBody);
723
724 }
725 private void button2_Click(object sender, EventArgs e)
726 {
727 if (cookiecontainer.Count == 0)
728 {
729 Form formdia = new Form();
730 formdia.ShowDialog();
731
732 MessageBoxButtons messButton = MessageBoxButtons.OK;
733 DialogResult dr = MessageBox.Show("请先登录后操作,OK ?", "提示", messButton);
734 if (dr == DialogResult.OK)//如果点击“确定”按钮
735 {
736 this.button2.Enabled = false;
737 this.button1.Enabled = true;
738 }
739 else//如果点击“取消”按钮
740 {
741 }
742 }
743 else
744 {
745 getPhotosinf(this.textBox4.Text);
746 }
747 }
748
749 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
750 {
751 if (this.Visible == true)
752 {
753 this.Hide();
754 }
755 else
756 {
757 this.Show();
758 this.Focus();
759 }
760 }
761
762 private void timer1_Tick(object sender, EventArgs e)
763 {
764 if (notifyIcon1.Text == "单击显示隐藏窗口")
765 {
766 notifyIcon1.Icon = new Icon("reload.ico");
767 notifyIcon1.Text = "单击显示隐藏窗口";
768 }
769 else
770 {
771 if (notifyIcon1.Text == "正在下载,请稍候...")
772 {
773 notifyIcon1.Icon = new Icon("reload2.ico");
774 notifyIcon1.Text = "正在下载,请稍候..";
775 }
776 else
777 {
778 notifyIcon1.Icon = new Icon("reload.ico");
779 notifyIcon1.Text = "正在下载,请稍候...";
780 }
781 }
782 }
783
784 private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
785 {
786 this.Show();
787 this.Focus();
788 }
789
790 private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
791 {
792 this.Hide();
793 }
794
795 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
796 {
797 this.Close();
798 }
799 }
800 }
2 using System.Drawing;
3 using System.Text;
4 using System.Windows.Forms;
5 using System.Net;
6 using System.IO;
7 using System.Text.RegularExpressions;
8 using System.Threading;
9
10 namespace WindowsApplication1
11 {
12 public partial class LoginForm : Form
13 {
14 /// <summary>
15 /// Required designer variable.
16 /// </summary>
17 private System.ComponentModel.IContainer components = null;
18 private TextBox textBox3;
19 public CookieContainer cookiecontainer = null;
20 string HtmlBody = "";
21 string bstr = "";
22 string estr = "";
23 string u_url = "";
24 string urlwebbrower = "";
25 string savepath = "c:\\";
26 Thread mythread;
27 private TextBox textBox4;
28 private Label label3;
29 private Label label4;
30 private TextBox textBox5;
31 private Button button2;
32 private FolderBrowserDialog folderBrowserDialog1;
33 private ProgressBar progressBar1;
34 private CheckBox checkBox1;
35 private NotifyIcon notifyIcon1;
36 private WebBrowser webBrowser1;
37 private ContextMenuStrip contextMenuStrip1;
38 private ToolStripMenuItem 显示ToolStripMenuItem;
39 private ToolStripMenuItem 隐藏ToolStripMenuItem;
40 private ToolStripMenuItem 退出ToolStripMenuItem;
41 private System.Windows.Forms.Timer timer1;
42 HttpWebRequest request = null;
43
44 /// <summary>
45 /// Clean up any resources being used.
46 /// </summary>
47 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
48 protected override void Dispose(bool disposing)
49 {
50 if (disposing && (components != null))
51 {
52 components.Dispose();
53 }
54 base.Dispose(disposing);
55 }
56
57 #region Windows Form Designer generated code
58
59 /// <summary>
60 /// Required method for Designer support - do not modify
61 /// the contents of this method with the code editor.
62 /// </summary>
63 private void InitializeComponent()
64 {
65 this.components = new System.ComponentModel.Container();
66 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LoginForm));
67 this.textBox1 = new System.Windows.Forms.TextBox();
68 this.textBox2 = new System.Windows.Forms.TextBox();
69 this.label1 = new System.Windows.Forms.Label();
70 this.label2 = new System.Windows.Forms.Label();
71 this.button1 = new System.Windows.Forms.Button();
72 this.textBox3 = new System.Windows.Forms.TextBox();
73 this.textBox4 = new System.Windows.Forms.TextBox();
74 this.label3 = new System.Windows.Forms.Label();
75 this.label4 = new System.Windows.Forms.Label();
76 this.textBox5 = new System.Windows.Forms.TextBox();
77 this.button2 = new System.Windows.Forms.Button();
78 this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
79 this.progressBar1 = new System.Windows.Forms.ProgressBar();
80 this.checkBox1 = new System.Windows.Forms.CheckBox();
81 this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
82 this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
83 this.显示ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
84 this.隐藏ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
85 this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
86 this.webBrowser1 = new System.Windows.Forms.WebBrowser();
87 this.timer1 = new System.Windows.Forms.Timer(this.components);
88 this.contextMenuStrip1.SuspendLayout();
89 this.SuspendLayout();
90 //
91 // textBox1
92 //
93 this.textBox1.Location = new System.Drawing.Point(77, 16);
94 this.textBox1.Name = "textBox1";
95 this.textBox1.Size = new System.Drawing.Size(100, 23);
96 this.textBox1.TabIndex = 0;
97 this.textBox1.Text = "568470306@qq.com";
98 //
99 // textBox2
100 //
101 this.textBox2.Location = new System.Drawing.Point(77, 54);
102 this.textBox2.Name = "textBox2";
103 this.textBox2.PasswordChar = '*';
104 this.textBox2.Size = new System.Drawing.Size(100, 23);
105 this.textBox2.TabIndex = 1;
106 this.textBox2.Text = "qinqinppz";
107 //
108 // label1
109 //
110 this.label1.AutoSize = true;
111 this.label1.Location = new System.Drawing.Point(13, 24);
112 this.label1.Name = "label1";
113 this.label1.Size = new System.Drawing.Size(56, 17);
114 this.label1.TabIndex = 2;
115 this.label1.Text = "用户名:";
116 //
117 // label2
118 //
119 this.label2.AutoSize = true;
120 this.label2.Location = new System.Drawing.Point(13, 58);
121 this.label2.Name = "label2";
122 this.label2.Size = new System.Drawing.Size(56, 17);
123 this.label2.TabIndex = 3;
124 this.label2.Text = "密 码:";
125 //
126 // button1
127 //
128 this.button1.Location = new System.Drawing.Point(214, 53);
129 this.button1.Name = "button1";
130 this.button1.Size = new System.Drawing.Size(75, 23);
131 this.button1.TabIndex = 4;
132 this.button1.Text = "确定";
133 this.button1.UseVisualStyleBackColor = true;
134 this.button1.Click += new System.EventHandler(this.button1_Click);
135 //
136 // textBox3
137 //
138 this.textBox3.Location = new System.Drawing.Point(12, 123);
139 this.textBox3.Multiline = true;
140 this.textBox3.Name = "textBox3";
141 this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
142 this.textBox3.Size = new System.Drawing.Size(830, 236);
143 this.textBox3.TabIndex = 6;
144 this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
145 //
146 // textBox4
147 //
148 this.textBox4.Location = new System.Drawing.Point(76, 90);
149 this.textBox4.Name = "textBox4";
150 this.textBox4.Size = new System.Drawing.Size(681, 23);
151 this.textBox4.TabIndex = 8;
152 this.textBox4.Text = "http://photo.renren.com/getalbum.do?id=347985504&owner=249664496&ref=sharenewsfee" +
153 "d";
154 //
155 // label3
156 //
157 this.label3.AutoSize = true;
158 this.label3.Location = new System.Drawing.Point(13, 93);
159 this.label3.Name = "label3";
160 this.label3.Size = new System.Drawing.Size(56, 17);
161 this.label3.TabIndex = 5;
162 this.label3.Text = "地 址:";
163 this.label3.Click += new System.EventHandler(this.label3_Click);
164 //
165 // label4
166 //
167 this.label4.AutoSize = true;
168 this.label4.Location = new System.Drawing.Point(213, 21);
169 this.label4.Name = "label4";
170 this.label4.Size = new System.Drawing.Size(20, 17);
171 this.label4.TabIndex = 9;
172 this.label4.Text = " ";
173 //
174 // textBox5
175 //
176 this.textBox5.Location = new System.Drawing.Point(307, 15);
177 this.textBox5.Multiline = true;
178 this.textBox5.Name = "textBox5";
179 this.textBox5.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
180 this.textBox5.Size = new System.Drawing.Size(450, 60);
181 this.textBox5.TabIndex = 10;
182 //
183 // button2
184 //
185 this.button2.Location = new System.Drawing.Point(767, 89);
186 this.button2.Name = "button2";
187 this.button2.Size = new System.Drawing.Size(75, 23);
188 this.button2.TabIndex = 11;
189 this.button2.Text = "OK";
190 this.button2.UseVisualStyleBackColor = true;
191 this.button2.Click += new System.EventHandler(this.button2_Click);
192 //
193 // progressBar1
194 //
195 this.progressBar1.Location = new System.Drawing.Point(12, 562);
196 this.progressBar1.Name = "progressBar1";
197 this.progressBar1.Size = new System.Drawing.Size(830, 20);
198 this.progressBar1.Step = 1;
199 this.progressBar1.Style = System.Windows.Forms.ProgressBarStyle.Continuous;
200 this.progressBar1.TabIndex = 12;
201 //
202 // checkBox1
203 //
204 this.checkBox1.AutoSize = true;
205 this.checkBox1.Location = new System.Drawing.Point(767, 62);
206 this.checkBox1.Name = "checkBox1";
207 this.checkBox1.Size = new System.Drawing.Size(75, 21);
208 this.checkBox1.TabIndex = 13;
209 this.checkBox1.Text = "保存评论";
210 this.checkBox1.UseVisualStyleBackColor = true;
211 //
212 // notifyIcon1
213 //
214 this.notifyIcon1.BalloonTipText = "最小化";
215 this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
216 this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
217 this.notifyIcon1.Text = "最小化";
218 this.notifyIcon1.Visible = true;
219 this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
220 //
221 // contextMenuStrip1
222 //
223 this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
224 this.显示ToolStripMenuItem,
225 this.隐藏ToolStripMenuItem,
226 this.退出ToolStripMenuItem});
227 this.contextMenuStrip1.Name = "contextMenuStrip1";
228 this.contextMenuStrip1.Size = new System.Drawing.Size(101, 70);
229 //
230 // 显示ToolStripMenuItem
231 //
232 this.显示ToolStripMenuItem.Name = "显示ToolStripMenuItem";
233 this.显示ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
234 this.显示ToolStripMenuItem.Text = "显示";
235 this.显示ToolStripMenuItem.Click += new System.EventHandler(this.显示ToolStripMenuItem_Click);
236 //
237 // 隐藏ToolStripMenuItem
238 //
239 this.隐藏ToolStripMenuItem.Name = "隐藏ToolStripMenuItem";
240 this.隐藏ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
241 this.隐藏ToolStripMenuItem.Text = "隐藏";
242 this.隐藏ToolStripMenuItem.Click += new System.EventHandler(this.隐藏ToolStripMenuItem_Click);
243 //
244 // 退出ToolStripMenuItem
245 //
246 this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
247 this.退出ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
248 this.退出ToolStripMenuItem.Text = "退出";
249 this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click);
250 //
251 // webBrowser1
252 //
253 this.webBrowser1.Location = new System.Drawing.Point(12, 365);
254 this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
255 this.webBrowser1.Name = "webBrowser1";
256 this.webBrowser1.Size = new System.Drawing.Size(830, 191);
257 this.webBrowser1.TabIndex = 7;
258 //
259 // timer1
260 //
261 this.timer1.Enabled = true;
262 this.timer1.Interval = 1000;
263 this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
264 //
265 // LoginForm
266 //
267 this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
268 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
269 this.BackColor = System.Drawing.SystemColors.Control;
270 this.ClientSize = new System.Drawing.Size(851, 588);
271 this.Controls.Add(this.checkBox1);
272 this.Controls.Add(this.progressBar1);
273 this.Controls.Add(this.button2);
274 this.Controls.Add(this.textBox5);
275 this.Controls.Add(this.label4);
276 this.Controls.Add(this.textBox4);
277 this.Controls.Add(this.webBrowser1);
278 this.Controls.Add(this.textBox3);
279 this.Controls.Add(this.label3);
280 this.Controls.Add(this.button1);
281 this.Controls.Add(this.label2);
282 this.Controls.Add(this.label1);
283 this.Controls.Add(this.textBox2);
284 this.Controls.Add(this.textBox1);
285 this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
286 this.ForeColor = System.Drawing.SystemColors.ControlText;
287 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
288 this.Name = "LoginForm";
289 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
290 this.Text = "校内……";
291 this.Load += new System.EventHandler(this.LoginForm_Load);
292 this.Resize += new System.EventHandler(this.LoginForm_Resize);
293
294 this.contextMenuStrip1.ResumeLayout(false);
295 this.ResumeLayout(false);
296 this.PerformLayout();
297
298 }
299
300 #endregion
301
302 private System.Windows.Forms.TextBox textBox1;
303 private System.Windows.Forms.TextBox textBox2;
304 private System.Windows.Forms.Label label1;
305 private System.Windows.Forms.Label label2;
306 private System.Windows.Forms.Button button1;
307
308 public LoginForm()
309 {
310 InitializeComponent();
311 this.button2.Enabled = false;
312 int count = this.Controls.Count * 2 + 2;
313 float[] factor = new float[count];
314 int i = 0;
315 factor[i++] = Size.Width;
316 factor[i++] = Size.Height;
317 foreach (Control ctrl in this.Controls)
318 {
319 factor[i++] = ctrl.Location.X / (float)Size.Width;
320 factor[i++] = ctrl.Location.Y / (float)Size.Height;
321 ctrl.Tag = ctrl.Size;
322 }
323 Tag = factor;
324
325 }
326 private void LoginForm_Resize(object sender, EventArgs e)
327 {
328 float[] scale = (float[])Tag;
329 int i = 2;
330
331 foreach (Control ctrl in this.Controls)
332 {
333 ctrl.Left = (int)(Size.Width * scale[i++]);
334 ctrl.Top = (int)(Size.Height * scale[i++]);
335 ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
336 ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
337 //每次使用的都是最初始的控件大小,保证准确无误。
338 }
339 }
340 /// <summary>
341 /// 应用程序的主入口点。
342 /// </summary>
343 //[STAThread]
344 //static void Main()
345 //{
346 // Application.Run(new LoginForm());
347 //}
348 //获取相册详细信息
349 private void getPhotosinf(string url)
350 {
351 this.folderBrowserDialog1.Description = "请选择保存输出图片的文件夹";
352 this.folderBrowserDialog1.ShowNewFolderButton = true;
353 this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.DesktopDirectory;
354 DialogResult result_path = folderBrowserDialog1.ShowDialog();
355 u_url = url;
356 if (result_path == DialogResult.OK)
357 {
358 notifyIcon1.Text = "正在下载,请稍候...";
359 string folderName = folderBrowserDialog1.SelectedPath;
360 if (folderName != "")
361 {
362 savepath = folderName;
363 this.Text = savepath;
364 }
365 //mythread = new Thread(new ThreadStart(DoWork));
366 mythread = new Thread(new ThreadStart(getData));
367 mythread.Start();
368 //getData();
369 }
370 }
371
372 public delegate void myDelegate(string name);
373 //private void DoWork()
374 //{
375 // myDelegate md = new myDelegate(getData);
376 // this.BeginInvoke(md,"11");
377 //}
378 //程序核心部分
379 private void getData()
380 {
381 string url = u_url;
382 string code = getHtmlCode(url);
383 string friendname = "";
384 string photosname = "";
385 string photoscount = "";
386 friendname = getinfo(code, "<input type=\"hidden\" id=\"fromname\" name=\"fromname\" id=\"fromname\" value=\"", "\" />", "([\\s\\S]*?)");
387 photosname = getinfo(code, "<input type=\"hidden\" id=\"title\" name=\"title\" value=\"", "\" />", "([\\s\\S]*?)");
388 photoscount = getinfo(code, "<div id=\"summary\" name=\"summary\" style=\"display:none\">", "</div>", "([\\s\\S]*?)");
389 string phoinf = "相册名称:【" + photosname + "】;\n照片数量:【" + photoscount + "】; \n相册所属ID:【" + friendname + "】";
390 this.textBox5.Text = "";
391 this.textBox5.AppendText(phoinf);
392
393 MatchCollection matchs = null;
394 this.textBox3.Text = "";
395 int num = 0;
396 for (int i = 0; i < (int.Parse(photoscount) + 16 - 1) / 16; i++)
397 {
398 if (url.IndexOf("?") != -1)
399 {
400 code = getHtmlCode(url + "&curpage=" + i);
401 }
402 else
403 {
404 code = getHtmlCode(url + "?curpage=" + i);
405 }
406 matchs = getinfos(code, "<span class=\"img\">\\n<a href=\"", "\">", "([\\s\\S]*?)");
407 this.textBox3.AppendText("正在获取第 " + (i + 1) + " 页\n");
408
409 foreach (Match m in matchs)
410 {
411 num++;
412 code = getHtmlCode(m.Groups[1].Value);
413 string imgurl = getinfo(getinfo(code, "var photo = {", "]};", "([\\s\\S]*?)").Replace(@"\/", "/"), "largeurl\":\"", "\",\"summary\"", "([\\s\\S]*?\\.[\\w]{3,5})");
414 string jutimingzi = getFilenameStr(getinfo(code, "<input type=\"hidden\" id=\"title\" name=\"title\" value=\"", "\" />", "([\\s\\S]*?)"));
415 this.textBox3.AppendText(num + "-->>\t" + imgurl + "\n");
416 string[] filenames = imgurl.Split('/');
417 string filename = savepath + "\\" + num + "_" + (i + 1) + "_" + jutimingzi + filenames[filenames.Length - 1];
418 if (this.checkBox1.Checked)
419 {
420 string strcontent = getUnicode(getinfo(code, "var photo = {", "]};", "([\\s\\S]*?)").Replace(@"\/", "/"));
421 string strcontentfilename = filename.Split('.')[0] + ".txt";
422 saveFileTxt(strcontentfilename, strcontent);
423 }
424 DownloadFile(imgurl, filename);
425 urlwebbrower += "<img src=\"" + imgurl + "\" />";
426
427 }
428 }
429 this.webBrowser1.DocumentText = urlwebbrower;
430 notifyIcon1.Text = "单击显示隐藏窗口";
431 }
432 //获取用户真实姓名
433 private string realName(string htmlbody)
434 {
435 bstr = "<title>";
436 estr = "</title>";
437 string _regex = bstr+"([\\s\\S]*?)"+estr;
438 Match titlematch = Regex.Match(htmlbody, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
439 bstr = "";
440 estr = "";
441 return titlematch.Groups[1].Value.Trim().Split('-')[titlematch.Groups[1].Value.Trim().Split('-').Length-1];
442 }
443 //根据开始结束字符串得到匹配项
444 private string getinfo(string str0, string str1, string str2, string regex)
445 {
446 bstr = str1;
447 estr = str2;
448 string _regex = bstr + regex + estr;
449 Match match = Regex.Match(str0, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
450 bstr = "";
451 estr = "";
452 return match.Groups[1].Value;
453 }
454 //根据开始结束字符串得到匹配项(集合)
455 private MatchCollection getinfos(string str0, string str1, string str2, string regex)
456 {
457 bstr = str1;
458 estr = str2;
459 string _regex = bstr + regex + estr;
460 //str0 = str0.Replace(@"\n", "");
461 MatchCollection matchs = Regex.Matches(str0, _regex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
462 bstr = "";
463 estr = "";
464 return matchs;
465 }
466 //构造表单,模拟登录
467 private string activeLogin(string username,string password,string indexurl)
468 {
469 cookiecontainer = null;
470 cookiecontainer= new CookieContainer();
471 request = (HttpWebRequest)WebRequest.Create(indexurl);
472 string actiondata = "email=" + username + "&password=" + password;
473 byte[] b = System.Text.Encoding.UTF8.GetBytes(actiondata);
474 Encoding code = Encoding.GetEncoding("UTF-8");
475 request.CookieContainer = cookiecontainer;
476
477 request.Method = "POST";
478 request.KeepAlive = false;
479 request.ContentType = "application/x-www-form-urlencoded";
480 request.CookieContainer = cookiecontainer;
481 // 提交请求数据
482 System.IO.Stream outputStream = request.GetRequestStream();
483 outputStream.Write(b, 0, b.Length);
484 outputStream.Close();
485 // 接收返回的页面
486 HttpWebResponse response = request.GetResponse() as HttpWebResponse;
487 StreamReader sr = new StreamReader(response.GetResponseStream(),code);
488 string result = sr.ReadToEnd();
489 string rl = "";
490 while((rl = sr.ReadLine())!=null )
491 {
492 result += rl;
493 }
494 foreach (Cookie cookie in response.Cookies)
495 cookiecontainer.Add(cookie);
496 this.webBrowser1.Show();
497 //this.webBrowser1.Url = response.ResponseUri;
498 //this.webBrowser1.DocumentText = result;
499 return "【" + cookiecontainer .Count+ "】" + result;
500 }
501
502 /// <summary>
503 ///获取校内的登录页地址
504 /// </summary>
505 /// <param name="indexUrl"></param>
506 /// <returns></returns>
507 private string actionUrl(string indexUrl)
508 {
509 string index = indexUrl;
510 Encoding code = Encoding.GetEncoding("UTF-8");
511 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.renren.com");
512 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
513 StreamReader sr = new StreamReader(response.GetResponseStream(), code);
514 string str = sr.ReadLine();
515
516 string hyperlink1= "<form method=\"post\" id=\"loginForm\" class=\"login-form\" action=\"http://www.renren.com/PLogin.do\" >";
517 Regex reg = new Regex("http(s)?://([\\w]+\\.)?([\\w]+\\.)?([\\w]+)?/([\\w]+\\.)?([\\w]+)?");
518 Match m=reg.Match(hyperlink1);
519 string a=m.Value;
520 return a;
521 }
522 //获取请求的地址的源码
523 public string getHtmlCode(String url)
524 {
525 request = (HttpWebRequest)WebRequest.Create(url);
526 request.CookieContainer = cookiecontainer;
527 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
528 Stream stream = response.GetResponseStream();
529 StreamReader sr = new StreamReader(stream, Encoding.UTF8);
530 String text = sr.ReadToEnd();
531 return text;
532 }
533 //将评论保存为文本文件
534 private void saveFileTxt(string filename,string strcontent)
535 {
536 //if (!File.Exists(filename))
537 //{
538 // File.Create(filename);
539 //}
540 //处理评论部分的UNICODE编码
541
542 string outstr = "";
543 MatchCollection matchs = getinfos(strcontent, "\"body\":\"<span>", "</span>\",\"author\"", "([\\s\\S]*?)");
544 //将评论正序输出
545 //foreach(Match m in matchs)
546 //{
547 // string _str = m.Groups[1].Value.Replace(@"\r\n", "\r\n") + "\r\n";
548 // _str = Regex.Replace(_str, @"</?[a-z]+[\s\S]*?/?>","");
549 // outstr += _str;
550 //}
551
552 //将评论倒叙输出
553 for (int i = matchs.Count-1; i >=0;i-- )
554 {
555 string _str = matchs[i].Groups[1].Value.Replace(@"\r\n", "\r\n") + "\r\n";
556 _str = Regex.Replace(_str, @"</?[a-z]+[\s\S]*?/?>", "");
557 outstr += _str;
558 }
559 StreamWriter sw = new StreamWriter(filename);
560 sw.Write(outstr);
561 sw.Flush();
562 sw.Close();
563 sw.Dispose();
564 }
565 //匹配UNICODE字符串
566 private string getUnicode(string unicodestr)
567 {
568 string outstr = "";
569 for (int i = 0; i < unicodestr.Length-7;)
570 {
571 string sss = unicodestr.Substring(i, 6);
572 if (Regex.IsMatch(unicodestr.Substring(i, 6), @"\\u[a-z0-9]{4}"))
573 {
574 outstr += unicodeToasc(unicodestr.Substring(i, 6));
575 i = i + 6;
576 }
577 else
578 {
579 outstr += unicodestr.Substring(i, 1);
580 i = i + 1;
581 }
582 }
583 return outstr;
584 }
585 //过滤文件名中的非法字符串,返回文件名中的前十个字符
586 private string getFilenameStr(string _filename)
587 {
588 string _str = _filename;
589 if (_str.Length >= 20)
590 {
591 _str = _str.Substring(0, 20);
592 }
593 _str = _str.Replace("\\", string.Empty);
594 _str = _str.Replace("/", string.Empty);
595 _str = _str.Replace(":", string.Empty);
596 _str = _str.Replace("*", string.Empty);
597 _str = _str.Replace("?", string.Empty);
598 _str = _str.Replace("\"", string.Empty);
599 _str = _str.Replace("<", string.Empty);
600 _str = _str.Replace(">", string.Empty);
601 _str = _str.Replace("|", string.Empty);
602 _str = _str.Replace("\r\n", "");
603 _str = _str.Replace("\n", "");
604 _str = _str.Replace("\r", "");
605 //int ii = _str.IndexOf("\r\n");
606 _str = _str.Replace(" ", string.Empty); //前面的替换会产生空格,最后将其一并替换掉
607 return _str;
608 }
609 //UNICODE字符转为中文
610 private string unicodeToasc(string unicodestr)
611 {
612 unicodestr = unicodestr.Replace("\\u","\\");
613 string[] value1 = unicodestr.Split('\\');
614 string tempValue1 = "";
615 string tempValue2 = "";
616
617 string strOut = "";
618 foreach (string temp in value1)
619 {
620 if (temp.Length == 0)
621 {
622 continue;
623 }
624 try
625 {
626 tempValue1 = temp;
627 tempValue2 = "";
628 if (tempValue1.Length > 4)
629 {
630 tempValue1 = temp.Substring(0, 4);
631 tempValue2 = temp.Substring(4, temp.Length - 4);
632 }
633 int intASCII = Convert.ToInt32(tempValue1, 16);
634 strOut += ((char)intASCII).ToString() + tempValue2;
635 }
636 catch
637 {
638 strOut += temp;
639 }
640 }
641 return strOut;
642 }
643
644 /// <summary>
645 /// 下载文件
646 /// </summary>
647 /// <param name="URL">下载文件地址</param>
648 /// <param name="Filename">下载后的存放地址</param>
649 /// <param name="Prog">用于显示的进度条</param>
650 public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
651 {
652 try
653 {
654 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
655 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
656 long totalBytes = myrp.ContentLength;
657 if (prog != null)
658 {
659 prog.Maximum = (int)totalBytes;
660 }
661 System.IO.Stream st = myrp.GetResponseStream();
662 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
663 long totalDownloadedByte = 0;
664 byte[] by = new byte[1024];
665 int osize = st.Read(by, 0, (int)by.Length);
666 while (osize > 0)
667 {
668 totalDownloadedByte = osize + totalDownloadedByte;
669 so.Write(by, 0, osize);
670
671 if (prog != null)
672 {
673 prog.Value = (int)totalDownloadedByte;
674 }
675 osize = st.Read(by, 0, (int)by.Length);
676 }
677 so.Flush();
678 so.Close();
679 st.Close();
680 }
681 catch (System.Exception)
682 {
683 throw;
684 }
685 }
686 /// <summary>
687 /// 下载文件
688 /// </summary>
689 /// <param name="URL">下载文件地址</param>
690 /// <param name="Filename">下载后的存放地址</param>
691 public void DownloadFile(string URL, string filename)
692 {
693 DownloadFile(URL, filename,this.progressBar1);
694 }
695
696 private void label3_Click(object sender, EventArgs e)
697 {
698
699 }
700 private void textBox3_TextChanged(object sender, EventArgs e)
701 {
702
703 }
704
705 private void LoginForm_Load(object sender, EventArgs e)
706 {
707 notifyIcon1.Icon = new Icon("reload.ico");
708 notifyIcon1.Visible = true;
709 notifyIcon1.Text = "单击显示隐藏窗口";
710 }
711 private void button1_Click(object sender, EventArgs e)
712 {
713 string username = textBox1.Text;
714 string password = textBox2.Text;
715 HtmlBody = activeLogin(username, password, actionUrl(""));
716 this.textBox3.Text = HtmlBody;
717 if (cookiecontainer.Count == 9)
718 {
719 this.button1.Enabled = false;
720 this.button2.Enabled = true;
721 }
722 this.label4.Text = realName(HtmlBody);
723
724 }
725 private void button2_Click(object sender, EventArgs e)
726 {
727 if (cookiecontainer.Count == 0)
728 {
729 Form formdia = new Form();
730 formdia.ShowDialog();
731
732 MessageBoxButtons messButton = MessageBoxButtons.OK;
733 DialogResult dr = MessageBox.Show("请先登录后操作,OK ?", "提示", messButton);
734 if (dr == DialogResult.OK)//如果点击“确定”按钮
735 {
736 this.button2.Enabled = false;
737 this.button1.Enabled = true;
738 }
739 else//如果点击“取消”按钮
740 {
741 }
742 }
743 else
744 {
745 getPhotosinf(this.textBox4.Text);
746 }
747 }
748
749 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
750 {
751 if (this.Visible == true)
752 {
753 this.Hide();
754 }
755 else
756 {
757 this.Show();
758 this.Focus();
759 }
760 }
761
762 private void timer1_Tick(object sender, EventArgs e)
763 {
764 if (notifyIcon1.Text == "单击显示隐藏窗口")
765 {
766 notifyIcon1.Icon = new Icon("reload.ico");
767 notifyIcon1.Text = "单击显示隐藏窗口";
768 }
769 else
770 {
771 if (notifyIcon1.Text == "正在下载,请稍候...")
772 {
773 notifyIcon1.Icon = new Icon("reload2.ico");
774 notifyIcon1.Text = "正在下载,请稍候..";
775 }
776 else
777 {
778 notifyIcon1.Icon = new Icon("reload.ico");
779 notifyIcon1.Text = "正在下载,请稍候...";
780 }
781 }
782 }
783
784 private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
785 {
786 this.Show();
787 this.Focus();
788 }
789
790 private void 隐藏ToolStripMenuItem_Click(object sender, EventArgs e)
791 {
792 this.Hide();
793 }
794
795 private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
796 {
797 this.Close();
798 }
799 }
800 }