示例#1
0
 public static TransferResult Transfer(string url, string path, Credential credential, string pwd = null)
 {
     try
     {
         using (var wc = new CookieAwareWebClient())
         {
             wc.Cookies.Add(credential);
             var    str = wc.DownloadString(url);
             var    rurl = wc.ResponseUri.ToString();
             string shareid = null, uk = null;
             if (rurl.Contains("/share/init"))
             {
                 if (pwd == null)
                 {
                     throw new Exception("Need password.");
                 }
                 shareid = Regex.Match(rurl, "shareid=(\\d+)").Groups[1].Value;
                 uk      = Regex.Match(rurl, "uk=(\\d+)").Groups[1].Value;
                 wc.Headers.Add(HttpRequestHeader.Referer, rurl);
                 wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
                 var res = wc.UploadData("http://pan.baidu.com/share/verify?shareid=" + shareid + "&uk=" + uk, Encoding.UTF8.GetBytes("vcode=&vcode_str=&pwd=" + pwd));
                 var obj = JsonConvert.DeserializeObject <VerifyPwdResult>(Encoding.UTF8.GetString(res));
                 if (obj.errno != 0)
                 {
                     throw new Exception("Password verification returned errno = " + obj.errno);
                 }
                 str = wc.DownloadString(url);
             }
             str = Regex.Match(str, "yunData.setData(.*)").Groups[1].Value.Trim();
             str = str.Substring(1, str.Length - 3);
             var obj2 = JsonConvert.DeserializeObject <SharePageData>(str);
             str = "path=" + Uri.EscapeDataString(path) + "&filelist=[" + string.Join(",", obj2.file_list.list.Select(e => "\"" + Uri.EscapeDataString(e.path) + "\"")) + "]";
             wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
             wc.Headers.Add(HttpRequestHeader.Referer, url);
             var rand  = new Random();
             var logid = new string(Enumerable.Range(0, 100).Select(i => (char)('a' + rand.Next(26))).ToArray());
             var res2  = wc.UploadData("http://pan.baidu.com/share/transfer?channel=chunlei&clienttype=0&web=1&app_id=250528&ondup=newcopy&async=1&shareid=" + shareid + "&from=" + uk + "&logid=" + logid + "&bdstoken=" + obj2.bdstoken, Encoding.UTF8.GetBytes(str));
             var obj3  = JsonConvert.DeserializeObject <TransferResult>(Encoding.UTF8.GetString(res2));
             obj3.success = true;
             return(obj3);
         }
     }
     catch (Exception ex)
     {
         return(new TransferResult()
         {
             exception = ex
         });
     }
 }
        public static LoginResult Login(string username, string password, LoginCheckResult checkResult)
        {
            var result = new LoginResult();

            try
            {
                using (var wc = new CookieAwareWebClient())
                {
                    wc.Cookies.Add(checkResult.baiduid);
                    var ltoken = checkResult.ltoken;
                    var lstr   = "loginmerge=true&token=" + ltoken + "&tpl=netdisk&username="******"&password="******"&codestring=" + checkResult.codeString + "&verifycode=" + Uri.EscapeDataString(checkResult.verifyCode);
                    }
                    wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
                    var str   = Encoding.UTF8.GetString(wc.UploadData("https://passport.baidu.com/v2/api/?login", Encoding.UTF8.GetBytes(lstr)));
                    var match = Regex.Match(str, "error=(\\d+)");
                    var errno = match.Success ? int.Parse(match.Groups[1].Value) : 0;
                    if (errno != 0)
                    {
                        result.exception = new Exception("Login returned error = " + errno);
                        result.errno     = errno;
                    }
                    else
                    {
                        str = wc.DownloadString("https://passport.baidu.com/v3/login/api/auth/?return_type=3&tpl=netdisk&u=http%3A%2F%2Fpan.baidu.com%2Fdisk%2Fhome");
                        long uid = 0;
                        match = Regex.Match(str, "\"uk\"\\s*:\\s*(\\d+)");
                        if (match.Success)
                        {
                            long.TryParse(match.Groups[1].Value, out uid);
                        }
                        string baiduid = null, bduss = null, stoken = null;
                        foreach (Cookie cookie in wc.Cookies.GetAllCookies())
                        {
                            if (cookie.Name.ToLower() == "baiduid")
                            {
                                baiduid = cookie.Value;
                            }
                            else if (cookie.Name.ToLower() == "bduss")
                            {
                                bduss = cookie.Value;
                            }
                            else if (cookie.Name.ToLower() == "stoken" && cookie.Domain.ToLower().Contains("pan."))
                            {
                                stoken = cookie.Value;
                            }
                        }
                        if (baiduid != null && bduss != null && stoken != null)
                        {
                            result.credential = new Credential(baiduid, bduss, stoken, uid);
                            result.success    = true;
                        }
                        else
                        {
                            result.exception = new Exception("Cannot find required cookies.");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.exception = ex;
            }
            return(result);
        }