1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| package ml.lightly.demo.gradle.test;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject;
public class HttpUtil {
private JSONArray assembleJson() {
JSONObject json = new JSONObject(); JSONArray jsonArr = new JSONArray(); json.put("id", "zhangsan");
jsonArr.add(json); return jsonArr; }
private String getJsonValue(String response) { @SuppressWarnings("rawtypes") Map responseMap = (Map)JSON.parse(response); String value = (String)responseMap.get("message"); return value; }
private String doHttpPOSTSendJson(JSONArray jsonArr) { OutputStream out = null; BufferedReader reader = null; HttpURLConnection conn = null; StringBuilder response = new StringBuilder(); try { URL url = new URL("https://www.fastmock.site/mock/530cb59823947e8648f90191e9072021/easul/post_test"); conn = (HttpURLConnection)url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("connnection", "keep-alive"); conn.setUseCaches(false); conn.connect(); out = conn.getOutputStream(); out.write(jsonArr.toString().getBytes("utf-8")); out.flush(); reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String lines; while ((lines = reader.readLine()) != null) { response.append(lines); } Map<String, List<String>> headerMap = conn.getHeaderFields(); List<String> cookieList = headerMap.get("Set-Cookie"); StringBuilder builder = new StringBuilder(); for (String str : cookieList) { builder.append(str); } System.out.println(builder.toString()); for (String key : headerMap.keySet()) { System.out.println(key + "--->" + headerMap.get(key)); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } if (out != null) { out.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e){ e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } return response.toString(); }
public void httpTest() { JSONArray postDataArr = assembleJson(); String result = doHttpPOSTSendJson(postDataArr); System.out.println(result); String message = getJsonValue(result); System.out.println(message); } }
|