|
| 1 | +package com.blankj.subutil.util; |
| 2 | + |
| 3 | +import java.io.DataOutputStream; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStream; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Scanner; |
| 9 | + |
| 10 | +/** |
| 11 | + * Create by MilkZS on 2019/1/9 13:36 |
| 12 | + */ |
| 13 | +public class HttpsUtil { |
| 14 | + |
| 15 | + private static final int CONNECT_TIMEOUT_TIME = 15000; |
| 16 | + private static final int READ_TIMEOUT_TIME = 19000; |
| 17 | + |
| 18 | + /** |
| 19 | + * POST + JSON |
| 20 | + * |
| 21 | + * @param data send data |
| 22 | + * @param url target url |
| 23 | + * @return data receive from server |
| 24 | + * @author MilkZS |
| 25 | + */ |
| 26 | + public static String postJson(String data, String url) { |
| 27 | + return doHttpAction(data, true, true, url); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * POST + FORM |
| 32 | + * |
| 33 | + * @param data send data |
| 34 | + * @param url target url |
| 35 | + * @return data receive from serv |
| 36 | + * @author MilkZS |
| 37 | + */ |
| 38 | + public static String postForm(String data, String url) { |
| 39 | + return doHttpAction(data, false, true, url); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * GET + JSON |
| 44 | + * |
| 45 | + * @param data send data |
| 46 | + * @param url target url |
| 47 | + * @return data receive from server |
| 48 | + * @author MilkZS |
| 49 | + */ |
| 50 | + public static String getJson(String data, String url) { |
| 51 | + return doHttpAction(data, true, false, url); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * GET + FORM |
| 56 | + * |
| 57 | + * @param data send data |
| 58 | + * @param url target url |
| 59 | + * @return data receive from server |
| 60 | + * @author MilkZS |
| 61 | + */ |
| 62 | + public static String getForm(String data, String url) { |
| 63 | + return doHttpAction(data, false, false, url); |
| 64 | + } |
| 65 | + |
| 66 | + private static String doHttpAction(String data, boolean json, boolean post, String url) { |
| 67 | + HttpURLConnection connection = null; |
| 68 | + DataOutputStream os = null; |
| 69 | + InputStream is = null; |
| 70 | + try { |
| 71 | + URL sUrl = new URL(url); |
| 72 | + connection = (HttpURLConnection) sUrl.openConnection(); |
| 73 | + connection.setConnectTimeout(CONNECT_TIMEOUT_TIME); |
| 74 | + connection.setReadTimeout(READ_TIMEOUT_TIME); |
| 75 | + if (post) { |
| 76 | + connection.setRequestMethod("POST"); |
| 77 | + } else { |
| 78 | + connection.setRequestMethod("GET"); |
| 79 | + } |
| 80 | + //允许输入输出 |
| 81 | + connection.setDoInput(true); |
| 82 | + connection.setDoOutput(true); |
| 83 | + // 是否使用缓冲 |
| 84 | + connection.setUseCaches(false); |
| 85 | + // 本次连接是否处理重定向,设置成true,系统自动处理重定向; |
| 86 | + // 设置成false,则需要自己从http reply中分析新的url自己重新连接。 |
| 87 | + connection.setInstanceFollowRedirects(true); |
| 88 | + // 设置请求头里的属性 |
| 89 | + if (json) { |
| 90 | + connection.setRequestProperty("Content-Type", "application/json"); |
| 91 | + } else { |
| 92 | + connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| 93 | + connection.setRequestProperty("Content-Length", data.length() + ""); |
| 94 | + } |
| 95 | + connection.connect(); |
| 96 | + |
| 97 | + os = new DataOutputStream(connection.getOutputStream()); |
| 98 | + os.write(data.getBytes(), 0, data.getBytes().length); |
| 99 | + os.flush(); |
| 100 | + os.close(); |
| 101 | + |
| 102 | + is = connection.getInputStream(); |
| 103 | + Scanner scan = new Scanner(is); |
| 104 | + scan.useDelimiter("\\A"); |
| 105 | + if (scan.hasNext()) return scan.next(); |
| 106 | + } catch (Exception e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } finally { |
| 109 | + if (connection != null) connection.disconnect(); |
| 110 | + if (os != null) { |
| 111 | + try { |
| 112 | + os.close(); |
| 113 | + } catch (IOException e) { |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + } |
| 117 | + if (is != null) { |
| 118 | + try { |
| 119 | + is.close(); |
| 120 | + } catch (IOException e) { |
| 121 | + e.printStackTrace(); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return null; |
| 126 | + } |
| 127 | +} |
0 commit comments