城通网盘文件地址分析器

 
问题背景:闲来无事网上乱逛。一不小心就逛到了VC驿站,手贱啊!还是禁不住点了进去!虽然现在学习C#不想去看C++了,还是手贱点了进去。作为资源控的我,看到了一篇资源贴:
=========================================================================================================
棉猴老师套接字编程【全部免key】 尽快下载,晚了就关共享了
http://www.t00y.com/file/11381890 ;    第5-4次课 基于WSAEventSelect模型的五子棋程序2.rar
http://www.t00y.com/file/11381892 ;    第5-5次课 基于WSAEventSelect模型的五子棋程序3.rar
http://www.t00y.com/file/11381893 ;    第5-6次课 基于WSAEventSelect模型的五子棋程序4.rar
http://www.t00y.com/file/11381895 ;    第6-1次课 套接字重叠IO模型基础1.rar
http://www.t00y.com/file/11381898 ;    第6-2次课 套接字重叠IO模型基础2.rar
http://www.t00y.com/file/11381972 ;    第6-3次课 套接字重叠IO模型基础3.rar
http://www.t00y.com/file/11382000 ;    第6-4次课 网络词典代码分析1.rar
http://www.t00y.com/file/11382002 ;    第6-5次课 网络词典代码分析2.rar
http://www.t00y.com/file/11382053 ;    第6-6次课 网络词典代码分析3.rar
http://www.t00y.com/file/11382080 ;    第6-7次课 网络词典代码分析4.rar
http://www.t00y.com/file/11382083 ;    第7-1次课 完成端口模型基础1.rar
http://www.t00y.com/file/11382085 ;    第7-2次课 完成端口模型基础2.rar
http://www.t00y.com/file/11382086 ;    第7-3次课 基于完成端口模型学生考试系统1.rar
http://www.t00y.com/file/11382090 ;    第7-4次课 基于完成端口模型学生考试系统2.rar
http://www.t00y.com/file/11382093 ;    第7-5次课 基于完成端口模型学生考试系统3.rar
http://www.t00y.com/file/11381760 ;    第2-2次课 利用非阻塞套接字实现数据的发送和接收.rar
http://www.t00y.com/file/11381763 ;    第2-3次课 远程算数运算程序解析1.rar
http://www.t00y.com/file/11381765 ;    第2-4次课 远程算数运算程序解析2.rar
http://www.t00y.com/file/11381768 ;    第2-5次课 远程算数运算程序解析3.rar
http://www.t00y.com/file/11381770 ;    第3-1次课 Select模型开发.rar
http://www.t00y.com/file/11381772 ;    第3-2次课 简单select模型.rar
http://www.t00y.com/file/11381775 ;    第3-3次课 简单select模型2.rar
http://www.t00y.com/file/11381777 ;    第3-4次课 基于select模型的远程文件下载1.rar
http://www.t00y.com/file/11381842 ;    第3-5次课 基于select模型的远程文件下载2.rar
http://www.t00y.com/file/11381879 ;    第3-6次课 基于select模型的远程文件下载3.rar
http://www.t00y.com/file/11381880 ;    第4-1次课 WSAAsyncselect模型开发.rar
http://www.t00y.com/file/11381881 ;    第4-2次课 窗口例程对消息的处理.rar
http://www.t00y.com/file/11381882 ;    第4-3次课 WSAAsyncSelect模型小结.rar
http://www.t00y.com/file/11381886 ;    第5-2次课 WSAEventSelect模型基础2.rar
http://www.t00y.com/file/11381888 ;    第5-3次课 基于WSAEventSelect模型的五子棋程序1.rar
http://www.t00y.com/file/11381741 ;    select模型远程文件下载代码.rar
http://www.t00y.com/file/11381743 ;    第1-1次课 套接字基本函数1.rar
http://www.t00y.com/file/11381746 ;    第1-2次课 套接字基本函数2.rar
http://www.t00y.com/file/11381747 ;    第1-3次课 阻塞方式服务端编程1.rar
http://www.t00y.com/file/11381749 ;    第1-4次课 阻塞方式服务端编程2.rar
http://www.t00y.com/file/11381751 ;    第1-5次课 阻塞方式客户端编程1.rar
http://www.t00y.com/file/11381755 ;    第1-6次课 阻塞方式客户端编程2.rar
http://www.t00y.com/file/11381757 ;    第1-7次课 套接字阻塞方式大数据传输.rar
http://www.t00y.com/file/11381759 ;    第2-1次课 套接字非阻塞方式.rar 
=========================================================================================================
 尼玛这是要逆天啊!这么XX多!Shit!Oh My God!点了一个进去,资源发布在一个叫城通网盘的网站上。

图片

图片

且不说难以忍受的广告。这种页面跳转的风格就让人不爽。我实在难以忍受,决定来次数据的较量!用程序实现自动分析文件资源地址的功能。先看看实现的效果吧:
图片

只需要输入文件ID,例如:
http://www.t00y.com/file/11381743就只需要输入11381743,输入正确的验证码。就能获取资源文件的迅雷下载地址了。如果在加个批量分析导入文件ID,批量导出下载地址的功能使用起来就会更加方便。
下面简单说一下程序实现的关键点:
1.验证码Session ID的处理。
  验证码地址:http://www.ctdisk.com/guest_loginV2.php。每次对验证码请求时,服务端都会产生一个Session来记录正确的验证码。并且会返回一个Session ID储存在客户端的Cookie中。提交数据时会根据提交的Cookie中的Session ID去找到相应的Session进行验证码的比对。
使用WebClient取得Cookie的方法。请求返回后利用string cookie=wc.ResponseHeaders.Get("Set-Cookie")从返回http头文件的Set-Cookie行取得Cookie的值。提交数据时只需使用wc.Headers.Add("Cookie:" + cookie);向请求头中添加请求验证码返回的Cookie就Ok了。
2.验证码显示在winform窗体上。
  WebClient常用的请求资源的方法有DownLoadFile,DownLoadData,DownLoadString。
DownLoadFile,DownLoadData都能实现把验证码图片显示到winform中。在这里我选择用 DownLoadData获取验证码图片的byte[]数据流的方式。
1             WebClient wc = new WebClient();
2             Uri url = new Uri("http://www.ctdisk.com/randcodeV2.php");
3              //获取验证码图片byte[]数据流
4             var codedata = wc.DownloadData(url);
5             //通过byte[]数组创建内存流 
6             MemoryStream ms = new MemoryStream(codedata);
7           //通过内存流创建图片。并把图片显示到pictureBox上 
8             pictureBox1.Image = Image.FromStream(ms); 

 

下面是完整代码。
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.IO;
 7 using System.Linq;
 8 using System.Net;
 9 using System.Text;
10 using System.Text.RegularExpressions;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13  
14 namespace DownLoader
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22         string cookie = null;
23         private void btnGetCode_Click(object sender, EventArgs e)
24         {
25             WebClient wc = new WebClient();
26             Uri url = new Uri("http://www.ctdisk.com/randcodeV2.php");
27             //获取验证码图片byte[]数据
28             var codedata = wc.DownloadData(url);
29             //转换成内存流
30             MemoryStream ms = new MemoryStream(codedata);
31             //通过内存流创建图片并显示到PictureBox上
32             pictureBox1.Image = Image.FromStream(ms);
33             //从WebClient的ResponseHeaders中获取验证码Session ID的Cookie
34             cookie = wc.ResponseHeaders.Get("Set-Cookie");
35         }
36  
37         private void btnGetSource_Click(object sender, EventArgs e)
38         {
39             WebClient wc = new WebClient();
40             wc.Encoding = Encoding.UTF8;
41             Uri surl = new Uri("http://www.ctdisk.com/file/"+txtFileId.Text.Trim());
42             string html = wc.DownloadString(surl);
43             //先获取资源文件的hash_id,这个在验证时也会使用。hash_id与文件不对应,居然报的是验证码错误!呵呵!
44             string hash_id = Regex.Match(html,@"<input type=""hidden"" id=""hash_id"" name=""hash_id"" value=""(.+)""/>").Groups[1].Value;
45             Uri url = new Uri("http://www.ctdisk.com/guest_loginV2.php");
46             //Post提交数据必须的头
47             wc.Headers.Add("Content-Type: application/x-www-form-urlencoded");
48             wc.Headers.Add("Cookie:" + cookie);
49             //Post提交的数据
50             string poststr =
51                 String.Format(
52                 "file_id={0}&hash_id={1}&randcode={2}&Comfirm.x=153&Comfirm.y=20",
53                 txtFileId.Text.Trim(),hash_id, txtCode.Text.Trim());
54             //把Post提交数据转换成byte[]
55             byte[] postdata = Encoding.UTF8.GetBytes(poststr);
56             //Post提交数据并取得返回结果
57             string strhtml = Encoding.UTF8.GetString(wc.UploadData(url, "POST", postdata));
58             //对返回的数据进行处理
59             if (strhtml.Contains("验证码输入错误"))
60             {
61                 txtMsg.Text = "验证码错误或已失效!请从新输入!";
62                 this.btnGetCode_Click(sender,e);
63             }
64             else
65             {
66                 //正则匹配出资源文件下载地址
67                 Match m = Regex.Match(strhtml, @"thunderHref=""(thunder://.+)""");
68                 if (m.Length!=0)
69                 {
70                     txtMsg.Text = m.Groups[1].Value;
71                 }
72                 else
73                 {
74                     txtMsg.Text = "获取资源地址出错!请稍后重试!\r\n"+strhtml;
75                 }
76             }
77         }
78     }
79 }

 

 

posted @ 2013-02-08 14:44  duguao  阅读(2708)  评论(1编辑  收藏  举报