44using DownKyi . Core . BiliApi . Login ;
55using DownKyi . Core . Logging ;
66using DownKyi . Core . Settings ;
7+ using Newtonsoft . Json ;
78
89namespace DownKyi . Core . BiliApi ;
910
1011internal static class WebClient
1112{
12- private static string GetRandomBuvid3 ( )
13+ internal class SpiOrigin
1314 {
14- // 随机生成10位字符串
15- const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ;
16- var random = new Random ( ) ;
17- var result = new StringBuilder ( ) ;
18- for ( var i = 0 ; i < 10 ; i ++ )
19- {
20- result . Append ( str [ random . Next ( str . Length ) ] ) ;
21- }
15+ [ JsonProperty ( "data" ) ] public Spi ? Data { get ; set ; }
16+ public int Code { get ; set ; }
17+ public string ? Message { get ; set ; }
18+ }
19+
20+ internal class Spi
21+ {
22+ [ JsonProperty ( "b_3" ) ] public string ? Bvuid3 { get ; set ; }
23+ [ JsonProperty ( "b_4" ) ] public string ? Bvuid4 { get ; set ; }
24+ }
2225
23- return result . ToString ( ) ;
26+ private static string ? _bvuid3 = string . Empty ;
27+ private static string ? _bvuid4 = string . Empty ;
28+
29+ // private static string GetRandomBuvid3()
30+ // {
31+ // // 随机生成10位字符串
32+ // const string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
33+ // var random = new Random();
34+ // var result = new StringBuilder();
35+ // for (var i = 0; i < 10; i++)
36+ // {
37+ // result.Append(str[random.Next(str.Length)]);
38+ // }
39+ //
40+ // return result.ToString();
41+ // }
42+
43+ private static void GetBuvid ( )
44+ {
45+ const string url = "https://api.bilibili.com/x/frontend/finger/spi" ;
46+ var response = RequestWeb ( url ) ;
47+ var spi = JsonConvert . DeserializeObject < SpiOrigin > ( response ) ;
48+ _bvuid3 = spi ? . Data ? . Bvuid3 ;
49+ _bvuid4 = spi ? . Data ? . Bvuid4 ;
2450 }
2551
2652 /// <summary>
@@ -32,8 +58,7 @@ private static string GetRandomBuvid3()
3258 /// <param name="parameters"></param>
3359 /// <param name="retry"></param>
3460 /// <returns></returns>
35- public static string RequestWeb ( string url , string ? referer = null , string method = "GET" ,
36- Dictionary < string , string > ? parameters = null , int retry = 3 , bool needRandomBvuid3 = false )
61+ public static string RequestWeb ( string url , string ? referer = null , string method = "GET" , Dictionary < string , string > ? parameters = null , int retry = 3 )
3762 {
3863 // 重试次数
3964 if ( retry <= 0 )
@@ -62,6 +87,11 @@ public static string RequestWeb(string url, string? referer = null, string metho
6287
6388 try
6489 {
90+ if ( string . IsNullOrEmpty ( _bvuid3 ) && url != "https://api.bilibili.com/x/frontend/finger/spi" )
91+ {
92+ GetBuvid ( ) ;
93+ }
94+
6595 var request = ( HttpWebRequest ) WebRequest . Create ( url ) ;
6696 request . Method = method ;
6797 request . Timeout = 30 * 1000 ;
@@ -81,20 +111,19 @@ public static string RequestWeb(string url, string? referer = null, string metho
81111 // 构造cookie
82112 if ( ! url . Contains ( "getLogin" ) )
83113 {
84- request . Headers [ "origin" ] = "https://m .bilibili.com" ;
114+ request . Headers [ "origin" ] = "https://www .bilibili.com" ;
85115
86116 var cookies = LoginHelper . GetLoginInfoCookies ( ) ;
87- if ( cookies != null )
117+ request . CookieContainer = cookies ?? new CookieContainer ( ) ;
118+
119+ if ( ! string . IsNullOrEmpty ( _bvuid3 ) )
88120 {
89- request . CookieContainer = cookies ;
121+ request . CookieContainer . Add ( new Cookie ( "buvid3" , _bvuid3 , "/" , ".bilibili.com" ) ) ;
90122 }
91- else
123+
124+ if ( ! string . IsNullOrEmpty ( _bvuid4 ) )
92125 {
93- request . CookieContainer = new CookieContainer ( ) ;
94- if ( needRandomBvuid3 )
95- {
96- request . CookieContainer . Add ( new Cookie ( "buvid3" , GetRandomBuvid3 ( ) , "/" , ".bilibili.com" ) ) ;
97- }
126+ request . CookieContainer . Add ( new Cookie ( "buvid4" , _bvuid4 , "/" , ".bilibili.com" ) ) ;
98127 }
99128 }
100129
0 commit comments