Ejemplo n.º 1
0
        public static async Task<string> LoginBilibili(string UserName, string Password)
        {
            try
            {
                //发送第一次请求,得到access_key
                WebClient wc = new WebClient();
                string url = "https://api.bilibili.com/login?appkey=422fd9d7289a1dd9&platform=wp&pwd=" + WebUtility.UrlEncode(Password) + "&type=json&userid=" + WebUtility.UrlEncode(UserName);
                //url += "&sign=" + BiliApiHelper.GetSign(url);

                string results = await wc.DownloadStringTaskAsync(new Uri(url));
                //Json解析及数据判断
                LoginModel model = new LoginModel();
                model = JsonConvert.DeserializeObject<LoginModel>(results);
                if (model.code == -627)
                {
                    return "登录失败,密码错误!";
                }
                if (model.code == -626)
                {
                    return "登录失败,账号不存在!";
                }
                if (model.code == -625)
                {
                    return "密码错误多次";
                }
                if (model.code == -628)
                {
                    return "未知错误";
                }
                if (model.code == -1)
                {
                    return "登录失败,程序注册失败!请联系作者!";
                }
                HttpClient hc = new HttpClient();
                if (model.code == 0)
                {
                    access_key = model.access_key;
                    return model.access_key;
                    /*
                    
                    Windows.Web.Http.HttpResponseMessage hr2 = await hc.GetAsync(new Uri("http://api.bilibili.com/login/sso?&access_key=" + model.access_key + "&appkey=422fd9d7289a1dd9&platform=wp"));
                    hr2.EnsureSuccessStatusCode();
                    StorageFolder folder = ApplicationData.Current.LocalFolder;
                    StorageFile file = await folder.CreateFileAsync("us.bili", CreationCollisionOption.OpenIfExists);
                    await FileIO.WriteTextAsync(file, model.access_key);
                    */
                }

                return "err";
                //看看存不存在Cookie
                //TODO: 读取cookie http://www.cnblogs.com/suger/p/3359146.html
                /*
                hb = new HttpBaseProtocolFilter();
                HttpCookieCollection cookieCollection = hb.CookieManager.GetCookies(new Uri("http://bilibili.com/"));

                List<string> ls = new List<string>();
                foreach (HttpCookie item in cookieCollection)
                {
                    ls.Add(item.Name);
                }
                if (ls.Contains("DedeUserID"))
                {
                    return "登录成功";
                }
                else
                {
                    return "登录失败";
                }
                */
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2147012867)
                {
                    return "登录失败,检查你的网络连接!";
                }
                else
                {
                    return "登录发生错误";
                }

            }
        }
Ejemplo n.º 2
0
        public static async Task <string> LoginBilibili(string UserName, string Password)
        {
            try
            {
                //https://api.bilibili.com/login?appkey=422fd9d7289a1dd9&platform=wp&pwd=JPJclVQpH4jwouRcSnngNnuPEq1S1rizxVJjLTg%2FtdqkKOizeIjS4CeRZsQg4%2F500Oye7IP4gWXhCRfHT6pDrboBNNkYywcrAhbOPtdx35ETcPfbjXNGSxteVDXw9Xq1ng0pcP1burNnAYtNRSayEKC1jiugi1LKyWbXpYE6VaM%3D&type=json&userid=xiaoyaocz&sign=74e4c872ec7b9d83d3a8a714e7e3b4b3
                //发送第一次请求,得到access_key
                string url = "https://passport.bilibili.com/api/oauth2/login"; //+ WebUtility.UrlEncode(await GetEncryptedPassword(Password)) + " &type=json&userid=" + WebUtility.UrlEncode(UserName);
                //url += "&sign=" + GetSign(url);

                string content = string.Format("appkey={0}&build=411005&mobi_app=android&password={1}&platform=wp&ts={2}000&username={3}", _appKey, WebUtility.UrlEncode(await GetEncryptedPassword(Password)), GetTimeSpan, WebUtility.UrlEncode(UserName));
                content += "&sign=" + GetSign(content);
                string results = await PostResults(new Uri(url), content);

                //Json解析及数据判断
                LoginModel model = new LoginModel();
                model = JsonConvert.DeserializeObject <LoginModel>(results);
                if (model.code == -627)
                {
                    return("登录失败,密码错误!");
                }
                if (model.code == -626)
                {
                    return("登录失败,账号不存在!");
                }
                if (model.code == -625)
                {
                    return("密码错误多次");
                }
                if (model.code == -628)
                {
                    return("未知错误");
                }
                if (model.code == -1)
                {
                    return("登录失败,程序注册失败!请联系作者!");
                }

                if (model.code == 0)
                {
                    access_key = model.data.access_token;
                    //string urlgo = "http://api.bilibili.com/login/sso?gourl=http%3A%2F%2Fwww.bilibili.com&access_key=" + model.data.access_token + "&appkey=422fd9d7289a1dd9&platform=android&scale=xhdpi";
                    //urlgo += "&sign=" + GetSign(urlgo);
                    //try
                    //{
                    //    await WebClientClass.GetResults(new Uri(urlgo));
                    //}
                    //catch (Exception)
                    //{
                    //}
                    //SettingHelper.Set_Access_key(model.data.access_token);
                    return(model.data.access_token);
                }
                return("失败");
                //看看存不存在Cookie
                //HttpBaseProtocolFilter hb = new HttpBaseProtocolFilter();
                //HttpCookieCollection cookieCollection = hb.CookieManager.GetCookies(new Uri("http://bilibili.com/"));

                //List<string> ls = new List<string>();
                //foreach (HttpCookie item in cookieCollection)
                //{
                //    ls.Add(item.Name);
                //}
                //if (ls.Contains("DedeUserID"))
                //{
                //    return "登录成功";
                //}
                //else
                //{
                //    return "登录失败";
                //}
            }
            catch (Exception ex)
            {
                if (ex.HResult == -2147012867)
                {
                    return("登录失败,检查你的网络连接!");
                }
                else
                {
                    return("登录发生错误");
                }
            }
        }