66import com .coloryr .allmusic .client .core .player .decoder .flac .FlacDecoder ;
77import com .coloryr .allmusic .client .core .player .decoder .mp3 .Mp3Decoder ;
88import com .coloryr .allmusic .client .core .player .decoder .ogg .OggDecoder ;
9- import okhttp3 .*;
9+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
10+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
11+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
12+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
13+ import org .apache .hc .core5 .http .Header ;
14+ import org .apache .hc .core5 .http .HttpEntity ;
15+ import org .apache .hc .core5 .http .ParseException ;
16+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
1017
1118import java .net .SocketTimeoutException ;
1219import org .lwjgl .BufferUtils ;
@@ -27,10 +34,10 @@ public class AllMusicPlayer extends InputStream {
2734 private final Semaphore semaphore = new Semaphore (0 );
2835 private final Semaphore semaphore1 = new Semaphore (0 );
2936 private final Queue <ByteBuffer > queue = new ConcurrentLinkedQueue <>();
30- private OkHttpClient client ;
37+ private CloseableHttpClient client ;
3138 private String url ;
32- private Request request ;
33- private Response response ;
39+ private HttpGet request ;
40+ private CloseableHttpResponse response ;
3441 private InputStream content ;
3542 private boolean isClose = false ;
3643 private boolean reload = false ;
@@ -48,8 +55,7 @@ public AllMusicPlayer(IntBuffer source) {
4855 try {
4956 this .source = source ;
5057 new Thread (this ::run , "allmusic_run" ).start ();
51- client = new OkHttpClient .Builder ()
52- .build ();
58+ client = HttpClients .createDefault ();
5359 ScheduledExecutorService service = Executors .newSingleThreadScheduledExecutor ();
5460 service .scheduleAtFixedRate (this ::run1 , 0 , 10 , TimeUnit .MILLISECONDS );
5561 } catch (Exception e ) {
@@ -71,21 +77,18 @@ public String Get(String url) {
7177 if (url .contains ("https://music.163.com/song/media/outer/url?id=" )
7278 || url .contains ("http://music.163.com/song/media/outer/url?id=" )) {
7379 try {
74- Request request = new Request .Builder ()
75- .url (url )
76- .header ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52" )
77- .header ("Host" , "music.163.com" )
78- .build ();
79- Call call = client .newCall (request );
80- Response response = call .execute ();
81- if (response .code () == 302 ) {
82- String location = response .header ("Location" );
83- if (location != null ) {
84- response .close ();
85- return location ;
80+ HttpGet request = new HttpGet (url );
81+ request .setHeader ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52" );
82+ request .setHeader ("Host" , "music.163.com" );
83+ try (CloseableHttpResponse response = client .execute (request )) {
84+ int statusCode = response .getCode ();
85+ if (statusCode == 302 ) {
86+ Header locationHeader = response .getFirstHeader ("Location" );
87+ if (locationHeader != null ) {
88+ return locationHeader .getValue ();
89+ }
8690 }
8791 }
88- response .close ();
8992 return url ;
9093 } catch (Exception e ) {
9194 e .printStackTrace ();
@@ -116,20 +119,19 @@ public void set(int time) {
116119 public void connect () throws IOException {
117120 getClose ();
118121 streamClose ();
119- request = new Request .Builder ()
120- .url (url )
121- .header ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52" )
122- .header ("Range" , "bytes=" + local + "-" )
123- .build ();
124- response = client .newCall (request ).execute ();
125- if (!response .isSuccessful ()) {
126- throw new IOException ("Unexpected code " + response );
122+ request = new HttpGet (url );
123+ request .setHeader ("User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52" );
124+ request .setHeader ("Range" , "bytes=" + local + "-" );
125+ response = client .execute (request );
126+ int statusCode = response .getCode ();
127+ if (statusCode < 200 || statusCode >= 300 ) {
128+ throw new IOException ("Unexpected code " + statusCode );
127129 }
128- ResponseBody body = response .body ();
129- if (body == null ) {
130- throw new IOException ("Response body is null" );
130+ HttpEntity entity = response .getEntity ();
131+ if (entity == null ) {
132+ throw new IOException ("Response entity is null" );
131133 }
132- content = body . byteStream ();
134+ content = entity . getContent ();
133135 }
134136
135137 private void run () {
@@ -302,10 +304,13 @@ public void setMusic(String url) {
302304 }
303305
304306 private void getClose () {
305- // OkHttp doesn't have an abort method for requests, but we can cancel the call
306- // Since we store the response, we can close it
307+ // Close the HTTP response if it exists
307308 if (response != null ) {
308- response .close ();
309+ try {
310+ response .close ();
311+ } catch (IOException e ) {
312+ e .printStackTrace ();
313+ }
309314 response = null ;
310315 }
311316 request = null ;
0 commit comments