22
33import com .google .gson .Gson ;
44import com .mojang .authlib .minecraft .client .ObjectMapper ;
5+ import org .apache .http .client .methods .HttpGet ;
6+ import org .apache .http .impl .client .CloseableHttpClient ;
7+ import org .apache .http .impl .client .HttpClients ;
8+ import org .apache .http .util .EntityUtils ;
59import org .thinkingstudio .randomtitlerework .RandomTitleReworkMod ;
610
7- import java .io .BufferedReader ;
811import java .io .IOException ;
9- import java .io .InputStreamReader ;
10- import java .net .HttpURLConnection ;
11- import java .net .URL ;
1212
1313public class HitokotoClient {
14- private static final String API_URL = "https://v1.hitokoto.cn" ;
14+ private static final String API_URL = "https://v1.hitokoto.cn/ " ;
1515 private final ObjectMapper objectMapper ;
1616
1717 public HitokotoClient () {
@@ -24,23 +24,10 @@ public HitokotoClient() {
2424 * @throws IOException 网络或解析异常
2525 */
2626 public Hitokoto getRandomHitokoto () throws IOException {
27- URL url = new URL (API_URL );
28- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
29- connection .setRequestMethod ("GET" );
30- connection .setRequestProperty ("User-Agent" , "Java Hitokoto Client" );
31- connection .setConnectTimeout (5000 );
32- connection .setReadTimeout (5000 );
33-
34- try (BufferedReader reader = new BufferedReader (new InputStreamReader (connection .getInputStream ()))) {
35- StringBuilder response = new StringBuilder ();
36- String line ;
37- while ((line = reader .readLine ()) != null ) {
38- response .append (line );
39- }
27+ try (CloseableHttpClient httpClient = HttpClients .createDefault ()) {
28+ String response = EntityUtils .toString (httpClient .execute (new HttpGet (API_URL )).getEntity ());
4029 RandomTitleReworkMod .LOGGER .info ("Hitokoto Response String: " + response );
41- return objectMapper .readValue (response .toString (), Hitokoto .class );
42- } finally {
43- connection .disconnect ();
30+ return objectMapper .readValue (response , Hitokoto .class );
4431 }
4532 }
4633}
0 commit comments