Docker文档地址:https://any-api.com/docker_com/engine/docs/Definitions/TaskSpec
    Docker Hub API文档地址:https://docs.docker.com/docker-hub/api/latest/#
    Docker Engine API 文档地址:https://docs.docker.com/engine/api/sdk/
    下面代码使用Java实现了部分Docker API接口功能:

    1. package Docker;
    2. import org.json.JSONArray;
    3. import org.json.JSONException;
    4. import org.json.JSONObject;
    5. import java.io.BufferedReader;
    6. import java.io.IOException;
    7. import java.io.InputStreamReader;
    8. import java.io.PrintWriter;
    9. import java.net.HttpURLConnection;
    10. import java.net.URL;
    11. import java.net.URLConnection;
    12. import java.util.List;
    13. import java.util.Map;
    14. public class DockerAPI {
    15. public static void main(String[] args) throws JSONException {
    16. SetDockerHostAddr("http://192.168.20.148:2375");
    17. Docker_Services_create("nginx", "nginx:latest", 1,8085, 80);
    18. JSONArray array = Docker_Services_1();
    19. String id = array.getJSONObject(0).get("ID").toString();
    20. System.out.printf("ID = %s\n", id);
    21. Docker_Services_delete(id);
    22. }
    23. public static void Test_DockerAPI() throws JSONException {
    24. // 设置请求地址
    25. SetDockerHostAddr("http://192.168.20.148:2375");
    26. // 初始化Swarm
    27. Docker_Swarm_init("192.168.20.148:2377", 4789);
    28. // 获取服务列表
    29. Docker_Services_1();
    30. Docker_Services_1("6jz5z5d2o8a5");
    31. // 创建服务
    32. Docker_Services_create("nginx", "nginx:latest", 1,8085, 80);
    33. // 删除服务
    34. Docker_Services_delete("jyudhbtsqqtw");
    35. // 列出容器
    36. Docker_Containers_list();
    37. // 检查容器
    38. Docker_Containers_1("67e99b224f2d");
    39. // 启动容器
    40. Docker_Containers_start("67e99b224f2d");
    41. // 停止容器
    42. Docker_Containers_stop("67e99b224f2d", 3);
    43. // 重启容器
    44. Docker_Containers_restart("67e99b224f2d", 3);
    45. // 杀死容器
    46. Docker_Containers_kill("67e99b224f2d");
    47. // 暂停容器
    48. Docker_Containers_pause("67e99b224f2d");
    49. // 恢复容器
    50. Docker_Containers_unpause("67e99b224f2d");
    51. // 查看集群
    52. JSONObject obj_swarm = Docker_Swarm_1();
    53. System.out.printf("ID = %s\n", obj_swarm.get("ID").toString());
    54. // 加入集群
    55. SetDockerHostAddr("http://192.168.20.148:2375");
    56. Docker_Swarm_join("192.168.20.148:2375", "192.168.20.222:2377", "SWMTKN-1-3wygqrwkee9kg1x902hrotbn0j8monb55dwu9f810dlyf7t8l2-ac7hzwwnn92176bjolvr36fbc");
    57. // 查询节点
    58. Docker_Nodes_1("xuza3s1zc2ieybiro77l5hnm1");
    59. // 获取镜像列表
    60. Docker_Image_list();
    61. // 镜像检查
    62. Docker_Image_1("redis");
    63. // Ping
    64. Docker_ping();
    65. // 获取版本信息
    66. Docker_version();
    67. // 获取Info信息
    68. Docker_info();
    69. // 获取系统信息
    70. Docker_System_df();
    71. // 获取配置信息
    72. Docker_Configs_1();
    73. // 离开Swarm
    74. Docker_Swarm_leave();
    75. }
    76. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    77. private static String HostAddr;
    78. public static void SetDockerHostAddr(String hostAddr) {
    79. HostAddr = hostAddr; // "http://192.168.20.222:2375";
    80. }
    81. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    82. public static String Docker_Swarm_init(String addr, Integer port) throws JSONException {
    83. System.out.printf("-->> [Docker_Swarm_init]\n");
    84. System.out.printf("-->> [Request way] : POST\n");
    85. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/init");
    86. JSONObject obj = new JSONObject();
    87. obj.put("ListenAddr", addr); // "192.168.20.222:2377"
    88. obj.put("AdvertiseAddr", addr);
    89. obj.put("DataPathPort", port); // 4789
    90. obj.put("DefaultAddrPool", new JSONArray().put("10.10.0.0/8").put("20.20.0.0/8"));
    91. obj.put("SubnetSize", 24);
    92. obj.put("ForceNewCluster", false);
    93. JSONObject obj_spec = new JSONObject();
    94. obj_spec.put("Orchestration", new JSONObject());
    95. obj_spec.put("Raft", new JSONObject());
    96. obj_spec.put("Dispatcher", new JSONObject());
    97. obj_spec.put("CAConfig", new JSONObject());
    98. obj_spec.put("EncryptionConfig", new JSONObject().put("AutoLockManagers", false));
    99. obj.put("Spec", obj_spec);
    100. System.out.printf("-->> [Request parameters] : %s\n", obj.toString());
    101. String response = SendPost(HostAddr + "/swarm/init", obj.toString());
    102. System.out.printf("-->> [Response data] : %s\n\n", response);
    103. return response;
    104. }
    105. public static JSONObject Docker_Swarm_1() throws JSONException {
    106. System.out.printf("-->> [Docker_Swarm_1]\n");
    107. System.out.printf("-->> [Request way] : GET\n");
    108. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm");
    109. System.out.printf("-->> [Request parameters] : \n");
    110. String response = SendGet(HostAddr + "/swarm", "");
    111. System.out.printf("-->> [Response data] : %s\n\n", response);
    112. JSONObject obj = new JSONObject(response);
    113. return obj;
    114. }
    115. public static void Docker_Swarm_leave() {
    116. System.out.printf("-->> [Docker_Swarm_leave]\n");
    117. System.out.printf("-->> [Request way] : POST\n");
    118. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/leave?force=true");
    119. System.out.printf("-->> [Request parameters] : \n");
    120. String response = SendPost(HostAddr + "/swarm/leave?force=true", "");
    121. System.out.printf("-->> [Response data] : %s\n\n", response);
    122. }
    123. public static void Docker_Swarm_join(String hostAddr, String nodeAddr, String joinToken) throws JSONException {
    124. System.out.printf("-->> [Docker_Swarm_join]\n");
    125. System.out.printf("-->> [Request way] : POST\n");
    126. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/swarm/join");
    127. JSONObject obj = new JSONObject();
    128. obj.put("ListenAddr", hostAddr);
    129. obj.put("AdvertiseAddr", hostAddr);
    130. obj.put("RemoteAddrs", new JSONArray().put(nodeAddr));
    131. obj.put("JoinToken", joinToken);
    132. System.out.printf("-->> [Request parameters] : %s\n", obj.toString());
    133. String response = SendPost(HostAddr + "/swarm/join", obj.toString());
    134. System.out.printf("-->> [Response data] : %s\n\n", response);
    135. }
    136. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    137. public static JSONArray Docker_Services_1() throws JSONException {
    138. System.out.printf("-->> [Docker_Services_1]\n");
    139. System.out.printf("-->> [Request way] : GET\n");
    140. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services");
    141. System.out.printf("-->> [Request parameters] : \n");
    142. String response = SendGet(HostAddr + "/services", "");
    143. System.out.printf("-->> [Response data] : %s\n\n", response);
    144. JSONArray array = new JSONArray(response);
    145. return array;
    146. }
    147. public static JSONObject Docker_Services_1(String id) throws JSONException {
    148. System.out.printf("-->> [Docker_Services_1]\n");
    149. System.out.printf("-->> [Request way] : GET\n");
    150. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/" + id.toString());
    151. System.out.printf("-->> [Request parameters] : \n");
    152. String response = SendGet(HostAddr + "/services/" + id.toString(), "");
    153. System.out.printf("-->> [Response data] : %s\n\n", response);
    154. JSONObject obj = new JSONObject(response);
    155. return obj;
    156. }
    157. public static void Docker_Services_create(String name, String image, Integer replicas, Integer published_port, Integer target_port) throws JSONException {
    158. System.out.printf("-->> [Docker_Services_create]\n");
    159. System.out.printf("-->> [Request way] : POST\n");
    160. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/create");
    161. JSONObject obj = new JSONObject();
    162. obj.put("Name", name); //nginx
    163. JSONObject obj_TaskTemplate = new JSONObject();
    164. JSONObject obj_ContainerSpec = new JSONObject();
    165. obj_ContainerSpec.put("Image", image.toString()); // nginx:alpine
    166. obj_ContainerSpec.put("Hosts", new JSONArray().put("10.10.10.10 host1").put("ABCD:EF01:2345:6789:ABCD:EF01:2345:6789 host2"));
    167. obj_TaskTemplate.put("ContainerSpec", obj_ContainerSpec);
    168. JSONObject obj_Limits = new JSONObject();
    169. obj_Limits.put("Limits", new JSONObject().put("NanoCPUs", 1000000).put("MemoryBytes", 104857600));
    170. obj_TaskTemplate.put("Resources", obj_Limits);
    171. obj.put("TaskTemplate", obj_TaskTemplate);
    172. JSONObject obj_Mode = new JSONObject();
    173. obj_Mode.put("Replicated", new JSONObject().put("Replicas", replicas)); // 1
    174. obj.put("Mode", obj_Mode);
    175. JSONArray obj_Ports = new JSONArray();
    176. obj_Ports.put(new JSONObject().put("Protocol", "tcp").put("PublishedPort", published_port).put("TargetPort", target_port));
    177. obj.put("EndpointSpec", new JSONObject().put("Ports", obj_Ports));
    178. System.out.printf("-->> [Request parameters] : %s\n", obj.toString());
    179. String response = SendPost(HostAddr + "/services/create", obj.toString());
    180. System.out.printf("-->> [Response data] : %s\n\n", response);
    181. }
    182. public static void Docker_Services_delete(String id) {
    183. System.out.printf("-->> [Docker_Services_delete]\n");
    184. System.out.printf("-->> [Request way] : DELETE\n");
    185. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/services/" + id.toString());
    186. System.out.printf("-->> [Request parameters] : \n");
    187. String response = SendDelete(HostAddr + "/services/" + id.toString());
    188. System.out.printf("-->> [Response data] : %s\n\n", response);
    189. }
    190. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    191. public static JSONArray Docker_Containers_list() throws JSONException {
    192. System.out.printf("-->> [Docker_Containers_list]\n");
    193. System.out.printf("-->> [Request way] : GET\n");
    194. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/json");
    195. System.out.printf("-->> [Request parameters] : \n");
    196. String response = SendGet(HostAddr + "/containers/json", "");
    197. System.out.printf("-->> [Response data] : %s\n\n", response);
    198. JSONArray array = new JSONArray(response);
    199. return array;
    200. }
    201. public static JSONObject Docker_Containers_1(String id) throws JSONException {
    202. System.out.printf("-->> [Docker_Containers_1]\n");
    203. System.out.printf("-->> [Request way] : GET\n");
    204. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id.toString() + "/json");
    205. System.out.printf("-->> [Request parameters] : \n");
    206. String response = SendGet(HostAddr + "/containers/" + id.toString() + "/json", "");
    207. System.out.printf("-->> [Response data] : %s\n\n", response);
    208. JSONObject obj = new JSONObject(response);
    209. return obj;
    210. }
    211. public static void Docker_Containers_start(String id) {
    212. System.out.printf("-->> [Docker_Container_start]\n");
    213. System.out.printf("-->> [Request way] : POST\n");
    214. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/start");
    215. JSONObject obj = new JSONObject();
    216. String response = SendPost(HostAddr + "/containers/" + id + "/start", obj.toString());
    217. System.out.printf("-->> [Response data] : %s\n\n", response);
    218. }
    219. public static void Docker_Containers_stop(String id, Integer second) {
    220. System.out.printf("-->> [Docker_Containers_stop]\n");
    221. System.out.printf("-->> [Request way] : POST\n");
    222. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/stop");
    223. JSONObject obj = new JSONObject();
    224. String response = SendPost(HostAddr + "/containers/" + id + "/stop?t=" + second.toString(), obj.toString());
    225. System.out.printf("-->> [Response data] : %s\n\n", response);
    226. }
    227. public static void Docker_Containers_restart(String id, Integer second) {
    228. System.out.printf("-->> [Docker_Containers_restart]\n");
    229. System.out.printf("-->> [Request way] : POST\n");
    230. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/restart");
    231. JSONObject obj = new JSONObject();
    232. String response = SendPost(HostAddr + "/containers/" + id + "/restart?t=" + second.toString(), obj.toString());
    233. System.out.printf("-->> [Response data] : %s\n\n", response);
    234. }
    235. public static void Docker_Containers_kill(String id) {
    236. System.out.printf("-->> [Docker_Containers_kill]\n");
    237. System.out.printf("-->> [Request way] : POST\n");
    238. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/kill");
    239. JSONObject obj = new JSONObject();
    240. String response = SendPost(HostAddr + "/containers/" + id + "/kill", obj.toString());
    241. System.out.printf("-->> [Response data] : %s\n\n", response);
    242. }
    243. public static void Docker_Containers_pause(String id) {
    244. System.out.printf("-->> [Docker_Containers_pause]\n");
    245. System.out.printf("-->> [Request way] : POST\n");
    246. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/pause");
    247. JSONObject obj = new JSONObject();
    248. String response = SendPost(HostAddr + "/containers/" + id + "/pause", obj.toString());
    249. System.out.printf("-->> [Response data] : %s\n\n", response);
    250. }
    251. public static void Docker_Containers_unpause(String id) {
    252. System.out.printf("-->> [Docker_Containers_unpause]\n");
    253. System.out.printf("-->> [Request way] : POST\n");
    254. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/containers/" + id + "/unpause");
    255. JSONObject obj = new JSONObject();
    256. String response = SendPost(HostAddr + "/containers/" + id + "/unpause", obj.toString());
    257. System.out.printf("-->> [Response data] : %s\n\n", response);
    258. }
    259. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    260. public static JSONObject Docker_Nodes_1(String id) throws JSONException {
    261. System.out.printf("-->> [Docker_Nodes_1]\n");
    262. System.out.printf("-->> [Request way] : GET\n");
    263. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/nodes" + id.toString());
    264. System.out.printf("-->> [Request parameters] : \n");
    265. String response = SendGet(HostAddr + "/nodes/" + id.toString(), "");
    266. System.out.printf("-->> [Response data] : %s\n\n", response);
    267. JSONObject obj = new JSONObject(response);
    268. return obj;
    269. }
    270. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    271. public static JSONArray Docker_Image_list() throws JSONException {
    272. System.out.printf("-->> [Docker_Image_list]\n");
    273. System.out.printf("-->> [Request way] : GET\n");
    274. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/json");
    275. System.out.printf("-->> [Request parameters] : \n");
    276. String response = SendGet(HostAddr + "/images/json", "");
    277. System.out.printf("-->> [Response data] : %s\n\n", response);
    278. JSONArray array = new JSONArray(response);
    279. return array;
    280. }
    281. public static JSONObject Docker_Image_1(String name_or_id) throws JSONException {
    282. System.out.printf("-->> [Docker_Image_1]\n");
    283. System.out.printf("-->> [Request way] : GET\n");
    284. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/" + name_or_id + "/json");
    285. System.out.printf("-->> [Request parameters] : \n");
    286. String response = SendGet(HostAddr + "/images/" + name_or_id + "/json", "");
    287. System.out.printf("-->> [Response data] : %s\n\n", response);
    288. JSONObject obj = new JSONObject(response);
    289. return obj;
    290. }
    291. public static void Docker_Image_export(String name) {
    292. System.out.printf("-->> [Docker_Image_export]\n");
    293. System.out.printf("-->> [Request way] : GET\n");
    294. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/images/" + name + "/get");
    295. System.out.printf("-->> [Request parameters] : \n");
    296. String response = SendGet(HostAddr + "/images/" + name + "/get", "");
    297. System.out.printf("-->> [Response data] : %s\n\n", response);
    298. }
    299. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    300. public static String Docker_ping() {
    301. System.out.printf("-->> [Docker_ping]\n");
    302. System.out.printf("-->> [Request way] : GET\n");
    303. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/_ping");
    304. System.out.printf("-->> [Request parameters] : \n");
    305. String response = SendGet(HostAddr + "/_ping", "");
    306. System.out.printf("-->> [Response data] : %s\n\n", response);
    307. return response;
    308. }
    309. public static JSONObject Docker_version() throws JSONException {
    310. System.out.printf("-->> [Docker_version]\n");
    311. System.out.printf("-->> [Request way] : GET\n");
    312. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/version");
    313. System.out.printf("-->> [Request parameters] : \n");
    314. String response = SendGet(HostAddr + "/version", "");
    315. System.out.printf("-->> [Response data] : %s\n\n", response);
    316. JSONObject obj = new JSONObject(response);
    317. return obj;
    318. }
    319. public static JSONObject Docker_info() throws JSONException {
    320. System.out.printf("-->> [Docker_info]\n");
    321. System.out.printf("-->> [Request way] : GET\n");
    322. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/info");
    323. System.out.printf("-->> [Request parameters] : \n");
    324. String response = SendGet(HostAddr + "/info", "");
    325. System.out.printf("-->> [Response data] : %s\n\n", response);
    326. JSONObject obj = new JSONObject(response);
    327. return obj;
    328. }
    329. public static JSONObject Docker_System_df() throws JSONException {
    330. System.out.printf("-->> [Docker_System_df]\n");
    331. System.out.printf("-->> [Request way] : GET\n");
    332. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/system/df");
    333. System.out.printf("-->> [Request parameters] : \n");
    334. String response = SendGet(HostAddr + "/system/df", "");
    335. System.out.printf("-->> [Response data] : %s\n\n", response);
    336. JSONObject obj = new JSONObject(response);
    337. return obj;
    338. }
    339. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    340. public static JSONArray Docker_Configs_1() throws JSONException {
    341. System.out.printf("-->> [Docker_Configs_1]\n");
    342. System.out.printf("-->> [Request way] : GET\n");
    343. System.out.printf("-->> [Request URL] : %s\n", HostAddr + "/configs");
    344. System.out.printf("-->> [Request parameters] : \n");
    345. String response = SendGet(HostAddr + "/configs", "");
    346. System.out.printf("-->> [Response data] : %s\n\n", response);
    347. JSONArray array = new JSONArray(response);
    348. return array;
    349. }
    350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    351. public static String SendGet(String url, String param) {
    352. String result = "";
    353. BufferedReader in = null;
    354. try {
    355. String urlNameString = url + "?" + param;
    356. URL realUrl = new URL(urlNameString);
    357. // 打开和URL之间的连接
    358. URLConnection connection = realUrl.openConnection();
    359. // 设置通用的请求属性
    360. connection.setRequestProperty("accept", "*/*");
    361. connection.setRequestProperty("connection", "Keep-Alive");
    362. connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSE 6.0; Windows NT 5.1;SV1)");
    363. // 建立实际的连接
    364. connection.connect();
    365. // 获取所有响应头字段
    366. Map<String, List<String>> map = connection.getHeaderFields();
    367. // // 遍历所有的响应头字段
    368. // for (String key : map.keySet()) {
    369. // System.out.println(key + "--->" + map.get(key));
    370. // }
    371. // 定义 BufferedReader输入流来读取URL的响应
    372. in = new BufferedReader(new InputStreamReader(
    373. connection.getInputStream()));
    374. String line;
    375. while ((line = in.readLine()) != null) {
    376. result += line;
    377. }
    378. } catch (Exception e) {
    379. System.out.println("GET Request Exception! " + e);
    380. e.printStackTrace();
    381. }
    382. // 使用finally块来关闭输入流
    383. finally {
    384. try {
    385. if (in != null) {
    386. in.close();
    387. }
    388. } catch (Exception e2) {
    389. e2.printStackTrace();
    390. }
    391. }
    392. return result;
    393. }
    394. public static String SendPost(String url, String param) {
    395. PrintWriter out = null;
    396. BufferedReader in = null;
    397. String result = "";
    398. try {
    399. URL realUrl = new URL(url);
    400. // 打开和URL之间的连接
    401. URLConnection conn = realUrl.openConnection();
    402. // 设置通用的请求属性
    403. conn.setRequestProperty("accept", "*/*");
    404. conn.setRequestProperty("connection", "Keep-Alive");
    405. conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSE 6.0; Windows NT 5.1;SV1)");
    406. // 发送POST请求必须设置如下两行
    407. conn.setDoOutput(true);
    408. conn.setDoInput(true);
    409. // 获取URLConnection对象对应的输出流
    410. out = new PrintWriter(conn.getOutputStream());
    411. // 发送请求参数
    412. out.print(param);
    413. // flush输出流的缓冲
    414. out.flush();
    415. // 定义BufferedReader输入流来读取URL的响应
    416. in = new BufferedReader(
    417. new InputStreamReader(conn.getInputStream()));
    418. String line;
    419. while ((line = in.readLine()) != null) {
    420. result += line;
    421. }
    422. } catch (Exception e) {
    423. System.out.println("POST Request Exception! " + e);
    424. e.printStackTrace();
    425. }
    426. //使用finally块来关闭输出流、输入流
    427. finally {
    428. try {
    429. if (out != null) {
    430. out.close();
    431. }
    432. if (in != null) {
    433. in.close();
    434. }
    435. } catch (IOException ex) {
    436. ex.printStackTrace();
    437. }
    438. }
    439. return result;
    440. }
    441. public static String SendDelete(String url) {
    442. PrintWriter out = null;
    443. BufferedReader in = null;
    444. String result = "";
    445. try {
    446. URL realUrl = new URL(url);
    447. HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
    448. conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    449. conn.setRequestMethod("DELETE");
    450. // System.out.println(conn.getRequestProperties());
    451. // 发送POST请求必须设置如下两行
    452. conn.setDoOutput(true);
    453. conn.setDoInput(true);
    454. // 获取URLConnection对象对应的输出流
    455. out = new PrintWriter(conn.getOutputStream());
    456. // flush输出流的缓冲
    457. out.flush();
    458. // 定义BufferedReader输入流来读取URL的响应
    459. in = new BufferedReader(
    460. new InputStreamReader(conn.getInputStream()));
    461. String line;
    462. while ((line = in.readLine()) != null) {
    463. result += line;
    464. }
    465. } catch (Exception e) {
    466. System.out.println("POST Request Exception! " + e);
    467. e.printStackTrace();
    468. }
    469. //使用finally块来关闭输出流、输入流
    470. finally {
    471. try {
    472. if (out != null) {
    473. out.close();
    474. }
    475. if (in != null) {
    476. in.close();
    477. }
    478. } catch (IOException ex) {
    479. ex.printStackTrace();
    480. }
    481. }
    482. return result;
    483. }
    484. }