private void StartByBaidu() { new Thread(() => { var share_uk = ""; var share_id = ""; var share_timestamp = ""; var share_sign = ""; var share_ctime = ""; var bdstoken = ""; var dir = ""; //获取基本信息 HttpLibSyncRequestItem item = new HttpLibSyncRequestItem(); item.Url = txtUrl.Text; var source = HttpLibSyncRequest.Get(item); var regex = new Regex(@"FileUtils\.(?<name>.*?)\=(?<val>.*?);", RegexOptions.IgnoreCase | RegexOptions.Multiline); MatchCollection mc = regex.Matches(source); foreach (Match m in mc) { if (m.Groups[1].ToString() == "share_uk") { share_uk = m.Groups[2].ToString().RegexReplace("\"", ""); } else if (m.Groups[1].ToString() == "share_id") { share_id = m.Groups[2].ToString().RegexReplace("\"", ""); } else if (m.Groups[1].ToString() == "share_timestamp") { share_timestamp = m.Groups[2].ToString().RegexReplace("\"", ""); } else if (m.Groups[1].ToString() == "share_sign") { share_sign = m.Groups[2].ToString().RegexReplace("\"", ""); } else if (m.Groups[1].ToString() == "share_ctime") { share_ctime = m.Groups[2].ToString().RegexReplace("\"", ""); } else if (m.Groups[1].ToString() == "bdstoken") { bdstoken = m.Groups[2].ToString().RegexReplace("\"", ""); } } //获取文件路径 dir = item.Url.Substring(item.Url.IndexOf("path") + 5); //获取列表信息 item = new HttpLibSyncRequestItem(); item.Url = string.Format("http://yun.baidu.com/share/list?channel=chunlei&clienttype=0&web=1&num=100&t={0}&page=1&dir={1}&uk={2}&shareid={3}&order=time&desc=1&_={4}&bdstoken={5}", share_ctime, dir, share_uk, share_id, share_timestamp, bdstoken); source = HttpLibSyncRequest.Get(item); var baiduDir = source.ToObject<DiscBaiduDir>(); List<string> fs_ids = new List<string>(); foreach (var file in baiduDir.list) { if (file.isdir==1) { continue; } fs_ids.Add(file.fs_id); } //获取下载地址 item = new HttpLibSyncRequestItem(); item.Method = "POST"; item.Cookie = "BAIDUID=7BAD66114FD298BEE366E10076ABB39B:FG=1; MCITY=-%3A; PANPSC=16099273001502937311%3AENVL4Mn25SjbMmGvcu5hU%2FD5Dz9eBf453%2FGnbuhFZZu0je4P03MFhEq7xbIsIsvhtlJtZsiNWgLh0HgCbjvXHg%3D%3D; PANWEB=1; Hm_lvt_773fea2ac036979ebb5fcc768d8beb67=1407922610,1407994831,1408003015,1408004122; Hm_lvt_adf736c22cd6bcc36a1d27e5af30949e=1407922590,1407922610,1407994831,1408004122; cflag=65535:1; Hm_lpvt_773fea2ac036979ebb5fcc768d8beb67=1408007070; Hm_lpvt_adf736c22cd6bcc36a1d27e5af30949e=1408007070; bdshare_firstime=1407994835127; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=1434_8057_7826_8123_7799_8036_8167_7416; recommendTime=guanjia2014-08-11 14:33; BDRCVFR[kXA_3xnUNdC]=aeXf-1x8UdYcs; Hm_lvt_b181fb73f90936ebd334d457c848c8b5=1408003016; Hm_lpvt_b181fb73f90936ebd334d457c848c8b5=1408003016; BDRCVFR[gltLrB7qNCt]=mk3SLVN4HKm"; item.Postdata = "fid_list=%5B" + string.Join(",", fs_ids.ToArray()) + "%5D"; item.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; item.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0"; item.Referer = string.Format("http://pan.baidu.com/share/link?shareid={0}&uk={1}", share_id, share_uk); item.Url = string.Format("http://yun.baidu.com/share/download?channel=chunlei&clienttype=0&web=1&uk={0}&shareid={1}×tamp={2}&sign={3}&ct=pcygj&cv=4.6.1.0&nozip=1&bdstoken={4}", share_uk, share_id, share_timestamp, share_sign, bdstoken); source = HttpLibSyncRequest.Get(item); if (source.IndexOf("vcode") > -1) { var vc = source.ToObject<DiscBaiduDir>(); frmVCode vcode = new frmVCode(vc.img); DialogResult result = vcode.ShowDialog(); if (result == DialogResult.OK) { item = new HttpLibSyncRequestItem(); item.Method = "POST"; item.Cookie = "BAIDUID=7BAD66114FD298BEE366E10076ABB39B:FG=1; MCITY=-%3A; PANPSC=16099273001502937311%3AENVL4Mn25SjbMmGvcu5hU%2FD5Dz9eBf453%2FGnbuhFZZu0je4P03MFhEq7xbIsIsvhtlJtZsiNWgLh0HgCbjvXHg%3D%3D; PANWEB=1; Hm_lvt_773fea2ac036979ebb5fcc768d8beb67=1407922610,1407994831,1408003015,1408004122; Hm_lvt_adf736c22cd6bcc36a1d27e5af30949e=1407922590,1407922610,1407994831,1408004122; cflag=65535:1; Hm_lpvt_773fea2ac036979ebb5fcc768d8beb67=1408007070; Hm_lpvt_adf736c22cd6bcc36a1d27e5af30949e=1408007070; bdshare_firstime=1407994835127; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; H_PS_PSSID=1434_8057_7826_8123_7799_8036_8167_7416; recommendTime=guanjia2014-08-11 14:33; BDRCVFR[kXA_3xnUNdC]=aeXf-1x8UdYcs; Hm_lvt_b181fb73f90936ebd334d457c848c8b5=1408003016; Hm_lpvt_b181fb73f90936ebd334d457c848c8b5=1408003016; BDRCVFR[gltLrB7qNCt]=mk3SLVN4HKm"; item.Postdata = "fid_list=%5B" + string.Join(",", fs_ids.ToArray()) + "%5D&input=" + vcode.VCode + "&vcode=" + vc.vcode; item.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; item.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:31.0) Gecko/20100101 Firefox/31.0"; item.Referer = string.Format("http://pan.baidu.com/share/link?shareid={0}&uk={1}", share_id, share_uk); item.Url = string.Format("http://yun.baidu.com/share/download?channel=chunlei&clienttype=0&web=1&uk={0}&shareid={1}×tamp={2}&sign={3}&ct=pcygj&cv=4.6.1.0&nozip=1&bdstoken={4}", share_uk, share_id, share_timestamp, share_sign, bdstoken); source = HttpLibSyncRequest.Get(item); } } regex = new Regex(@"(?<id>\w+).:.(?<url>http.*?)[,}]", RegexOptions.IgnoreCase); mc = regex.Matches(source); foreach (Match m in mc) { string id = m.Groups["id"].ToString(); string url = m.Groups["url"].ToString().RegexReplace("[\\\\,}\"]", ""); var file =GetDiscBaiduDirItem(id,baiduDir.list); string result = string.Format("{0}|{1}", file == null ? "验证码错误!" : file.server_filename, url); BeginInvoke(new MethodInvoker(() => { clbResult.Items.Add(result, true); })); } }).Start(); }
public static string Get(HttpLibSyncRequestItem objhttpItem) { #region 初始设置 //默认的编码 Encoding encoding = Encoding.UTF8; //读取流的对象 StreamReader reader = null; //HttpWebRequest对象用来发起请求 HttpWebRequest request = null; //获取影响流的数据对象 HttpWebResponse response = null; //需要返回的数据对象 string returnData = "String Error"; #region 验证证书 if (!string.IsNullOrEmpty(objhttpItem.CerPath)) { //这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。 ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.Url)); //创建证书文件 X509Certificate objx509 = new X509Certificate(objhttpItem.CerPath); //添加到请求里 request.ClientCertificates.Add(objx509); } else { //初始化对像,并设置请求的URL地址 request = (HttpWebRequest)WebRequest.Create(GetUrl(objhttpItem.Url)); } #endregion 验证证书 #region 设置代理 if (string.IsNullOrEmpty(objhttpItem.ProxyUserName) && string.IsNullOrEmpty(objhttpItem.ProxyPwd) && string.IsNullOrEmpty(objhttpItem.ProxyIp)) { //不需要设置 } else { //设置代理服务器 WebProxy myProxy = new WebProxy(objhttpItem.ProxyIp, false); //建议连接 myProxy.Credentials = new NetworkCredential(objhttpItem.ProxyUserName, objhttpItem.ProxyPwd); //给当前请求对象 request.Proxy = myProxy; //设置安全凭证 request.Credentials = CredentialCache.DefaultNetworkCredentials; } #endregion 设置代理 //请求方式Get或者Post request.Method = objhttpItem.Method; request.Timeout = objhttpItem.Timeout; request.ReadWriteTimeout = objhttpItem.ReadWriteTimeout; //Accept request.Accept = objhttpItem.Accept; //ContentType返回类型 request.ContentType = objhttpItem.ContentType; //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息 request.UserAgent = objhttpItem.UserAgent; #region 编码 if (string.IsNullOrEmpty(objhttpItem.Encoding) || objhttpItem.Encoding.ToLower().Trim() == "null") { //读取数据时的编码方式 encoding = Encoding.UTF8; } else { //读取数据时的编码方式 encoding = System.Text.Encoding.GetEncoding(objhttpItem.Encoding); } #endregion 编码 #region Cookie if (!string.IsNullOrEmpty(objhttpItem.Cookie)) { //Cookie request.Headers[HttpRequestHeader.Cookie] = objhttpItem.Cookie; } //设置Cookie if (objhttpItem.CookieCollection != null) { if (request.CookieContainer.Count == 0) { request.CookieContainer.Add(objhttpItem.CookieCollection); } else { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(objhttpItem.CookieCollection); } } #endregion Cookie //来源地址 request.Referer = objhttpItem.Referer; //是否执行跳转功能 request.AllowAutoRedirect = objhttpItem.Allowautoredirect; #region Post数据 //验证在得到结果时是否有传入数据 if (!string.IsNullOrEmpty(objhttpItem.Postdata) && request.Method.Trim().ToLower().Contains("post")) { byte[] buffer = encoding.GetBytes(objhttpItem.Postdata); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); } #endregion Post数据 //设置最大连接 if (objhttpItem.Connectionlimit > 0) { request.ServicePoint.ConnectionLimit = objhttpItem.Connectionlimit; } #endregion #region 读取数据 try { using (response = (HttpWebResponse)request.GetResponse()) { if (response.Cookies != null) { objhttpItem.CookieCollection = response.Cookies; } if (response.Headers["set-cookie"] != null) { objhttpItem.Cookie = response.Headers["set-cookie"]; } objhttpItem.Response = response; objhttpItem.Request = request; //从这里开始我们要无视编码了 if (encoding == null) { MemoryStream stream = new MemoryStream(); if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { objhttpItem.Reader = reader; //开始读取流并设置编码方式 //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(stream, 10240); //.net4.0以下写法 stream = GetMemoryStream(response.GetResponseStream()); } else { objhttpItem.Reader = reader; //response.GetResponseStream().CopyTo(stream, 10240); // .net4.0以下写法 stream = GetMemoryStream(response.GetResponseStream()); } byte[] RawResponse = stream.ToArray(); string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length); //<meta(.*?)charset([\s]?)=[^>](.*?)> Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline); string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty; charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty); if (charter.Length > 0) { charter = charter.ToLower().Replace("iso-8859-1", "gbk"); encoding = Encoding.GetEncoding(charter); } else { if (response.CharacterSet.ToLower().Trim() == "iso-8859-1") { encoding = Encoding.GetEncoding("gbk"); } else { if (string.IsNullOrEmpty(response.CharacterSet.Trim())) { encoding = Encoding.UTF8; } else { encoding = Encoding.GetEncoding(response.CharacterSet); } } } returnData = encoding.GetString(RawResponse); } else { if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)) { //开始读取流并设置编码方式 using (reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding)) { objhttpItem.Reader = reader; returnData = reader.ReadToEnd(); } } else { //开始读取流并设置编码方式 using (reader = new StreamReader(response.GetResponseStream(), encoding)) { objhttpItem.Reader = reader; returnData = reader.ReadToEnd(); } } } } } catch (WebException ex) { //这里是在发生异常时返回的错误信息 returnData = "String Error"; response = (HttpWebResponse)ex.Response; objhttpItem.Response = response; } if (objhttpItem.IsToLower) { returnData = returnData.ToLower(); } return returnData; #endregion }