分析过后,我们接着来做一下代码实现
三、获取流程实现
近期正巧看到了木鱼在自己博客里发布的一个HTTP客户端库,困扰于原生HttpWebRequest的我立马就下来用了用,效果果然不错,所以本项目的HTTP请求就由
FSLib.Network 类库来完成,语法差别不大,不太懂的地方可以回复。
添加类库可以使用nuget:
Install-Package network.fishlee.net
下面来写通过展示地址来获得下载连接的方法:
private async void GetDownLoadUrl(string fileUrl) { //建立HTTP客户端 var client = new HttpClient(); //用于接收验证码 StringBuilder Result; //验证码的字符数组 byte[] bytes; //用于接收下载链接 string downPath = string.Empty; //通过正则匹配到链接中的 fileid int id = Convert.ToInt32(Extract(fileUrl, "\d{7,}")); //有时会出现一级域名为ymhwp的情况,这里先做一下替换,以防不测 fileUrl = fileUrl.Replace("ymhwp", "yimuhe"); //创建展示页面请求 var filecontext = client.Create<string>(HttpMethod.Get, fileUrl); //发送请求 await filecontext.SendTask(); if (filecontext.IsValid())//如果请求成功 { //替换链接中的fil为down,使其成为下载页面地址 fileUrl = fileUrl.Replace("file", "down"); //创建下载页面请求 var downcontext = client.Create<string>(HttpMethod.Get, fileUrl); await downcontext.SendTask(); if (downcontext.IsValid()) { //设置初始返回值为0,表示验证码输入错误,即识别失败 int response = 0; while (response == 0)//循环识别,直到识别成功 { //创建验证码图片请求 var vcode = client.Create<Byte[]>(HttpMethod.Get, "http://www.yimuhe.com/n_downcode.php"); await vcode.SendTask(); if (vcode.IsValid()) { //将获取到的验证码图片存入字节数组中 bytes = vcode.Result; Result = new StringBuilder('