@@ -136,27 +136,38 @@ private static String detectBestSource() {
136136 */
137137 private static long testSourceSpeed (String apiUrl , String sourceName ) {
138138 long startTime = System .currentTimeMillis ();
139+ HttpURLConnection conn = null ;
139140 try {
140141 URL url = new URL (apiUrl );
141- HttpURLConnection conn = (HttpURLConnection ) url .openConnection ();
142+ conn = (HttpURLConnection ) url .openConnection ();
142143 conn .setConnectTimeout (3000 );
143144 conn .setReadTimeout (3000 );
144- conn .setRequestMethod ("HEAD " ); // 只测试连接,不下载内容
145+ conn .setRequestMethod ("GET " ); // 使用 GET 请求(Gitee API 不支持 HEAD)
145146 conn .setRequestProperty ("User-Agent" , "Mozilla/5.0 (compatible; EasyPostman/" + SystemUtil .getCurrentVersion () + ")" );
147+ conn .setRequestProperty ("Accept" , "application/json" );
146148
147149 int code = conn .getResponseCode ();
148150 long responseTime = System .currentTimeMillis () - startTime ;
149151
150152 if (code == 200 || code == 301 || code == 302 ) {
151- log .debug ("{} connection test successful: {} ms" , sourceName , responseTime );
153+ log .debug ("{} connection test successful: {} ms (HTTP {}) " , sourceName , responseTime , code );
152154 return responseTime ;
153155 } else {
154156 log .debug ("{} connection test failed with code: {}" , sourceName , code );
155157 return Long .MAX_VALUE ;
156158 }
157159 } catch (Exception e ) {
158- log .debug ("{} connection test failed: {}" , sourceName , e .getMessage ());
160+ long failedTime = System .currentTimeMillis () - startTime ;
161+ log .debug ("{} connection test failed after {} ms: {}" , sourceName , failedTime , e .getMessage ());
159162 return Long .MAX_VALUE ;
163+ } finally {
164+ if (conn != null ) {
165+ try {
166+ // 关闭连接,避免资源泄漏
167+ conn .disconnect ();
168+ } catch (Exception ignored ) {
169+ }
170+ }
160171 }
161172 }
162173
0 commit comments