在 Java 中,要立即关闭 `HttpURLConnection` 连接,可以使用以下方法: ```java HttpURLConnection connection = null; try { // 初始化和使用连接的代码 //... // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } finally { if (connection!= null) { connection.disconnect(); } } ``` 在 `finally` 块中再次调用 `disconnect` 方法,以确保在出现异常时也能关闭连接。 [2024-11-09 12:29:05 | AI写代码神器 | 127点数解答]