diff --git a/DownKyi.Core/BiliApi/Users/UserInfo.cs b/DownKyi.Core/BiliApi/Users/UserInfo.cs index f9e5427..edf2ee6 100644 --- a/DownKyi.Core/BiliApi/Users/UserInfo.cs +++ b/DownKyi.Core/BiliApi/Users/UserInfo.cs @@ -49,7 +49,7 @@ public static class UserInfo var query = WbiSign.ParametersToQuery(WbiSign.EncodeWbi(parameters)); var url = $"https://api.bilibili.com/x/space/wbi/acc/info?{query}"; const string referer = "https://www.bilibili.com"; - var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true); + var response = WebClient.RequestWeb(url, referer); try { diff --git a/DownKyi.Core/BiliApi/VideoStream/VideoStream.cs b/DownKyi.Core/BiliApi/VideoStream/VideoStream.cs index c9c8d8a..ca0156c 100644 --- a/DownKyi.Core/BiliApi/VideoStream/VideoStream.cs +++ b/DownKyi.Core/BiliApi/VideoStream/VideoStream.cs @@ -232,7 +232,7 @@ public static class VideoStream /// private static PlayUrl GetPlayUrl(string url) { - const string referer = "https://m.bilibili.com"; + const string referer = "https://www.bilibili.com"; var response = WebClient.RequestWeb(url, referer); try @@ -271,7 +271,7 @@ public static class VideoStream private static PlayUrl? GetPlayUrlWebPage(string url) { const string referer = "https://www.bilibili.com"; - var response = WebClient.RequestWeb(url, referer, needRandomBvuid3: true); + var response = WebClient.RequestWeb(url, referer); try { diff --git a/DownKyi.Core/BiliApi/WebClient.cs b/DownKyi.Core/BiliApi/WebClient.cs index 5e986d6..04398b5 100644 --- a/DownKyi.Core/BiliApi/WebClient.cs +++ b/DownKyi.Core/BiliApi/WebClient.cs @@ -4,23 +4,49 @@ using System.Text; using DownKyi.Core.BiliApi.Login; using DownKyi.Core.Logging; using DownKyi.Core.Settings; +using Newtonsoft.Json; namespace DownKyi.Core.BiliApi; internal static class WebClient { - private static string GetRandomBuvid3() + internal class SpiOrigin { - // 随机生成10位字符串 - const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; - var random = new Random(); - var result = new StringBuilder(); - for (var i = 0; i < 10; i++) - { - result.Append(str[random.Next(str.Length)]); - } + [JsonProperty("data")] public Spi? Data { get; set; } + public int Code { get; set; } + public string? Message { get; set; } + } - return result.ToString(); + internal class Spi + { + [JsonProperty("b_3")] public string? Bvuid3 { get; set; } + [JsonProperty("b_4")] public string? Bvuid4 { get; set; } + } + + private static string? _bvuid3 = string.Empty; + private static string? _bvuid4 = string.Empty; + + // private static string GetRandomBuvid3() + // { + // // 随机生成10位字符串 + // const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + // var random = new Random(); + // var result = new StringBuilder(); + // for (var i = 0; i < 10; i++) + // { + // result.Append(str[random.Next(str.Length)]); + // } + // + // return result.ToString(); + // } + + private static void GetBuvid() + { + const string url = "https://api.bilibili.com/x/frontend/finger/spi"; + var response = RequestWeb(url); + var spi = JsonConvert.DeserializeObject(response); + _bvuid3 = spi?.Data?.Bvuid3; + _bvuid4 = spi?.Data?.Bvuid4; } /// @@ -32,8 +58,7 @@ internal static class WebClient /// /// /// - public static string RequestWeb(string url, string? referer = null, string method = "GET", - Dictionary? parameters = null, int retry = 3, bool needRandomBvuid3 = false) + public static string RequestWeb(string url, string? referer = null, string method = "GET", Dictionary? parameters = null, int retry = 3) { // 重试次数 if (retry <= 0) @@ -62,6 +87,11 @@ internal static class WebClient try { + if (string.IsNullOrEmpty(_bvuid3) && url != "https://api.bilibili.com/x/frontend/finger/spi") + { + GetBuvid(); + } + var request = (HttpWebRequest)WebRequest.Create(url); request.Method = method; request.Timeout = 30 * 1000; @@ -81,20 +111,19 @@ internal static class WebClient // 构造cookie if (!url.Contains("getLogin")) { - request.Headers["origin"] = "https://m.bilibili.com"; + request.Headers["origin"] = "https://www.bilibili.com"; var cookies = LoginHelper.GetLoginInfoCookies(); - if (cookies != null) + request.CookieContainer = cookies ?? new CookieContainer(); + + if (!string.IsNullOrEmpty(_bvuid3)) { - request.CookieContainer = cookies; + request.CookieContainer.Add(new Cookie("buvid3", _bvuid3, "/", ".bilibili.com")); } - else + + if (!string.IsNullOrEmpty(_bvuid4)) { - request.CookieContainer = new CookieContainer(); - if (needRandomBvuid3) - { - request.CookieContainer.Add(new Cookie("buvid3", GetRandomBuvid3(), "/", ".bilibili.com")); - } + request.CookieContainer.Add(new Cookie("buvid4", _bvuid4, "/", ".bilibili.com")); } }