get请求和post请求实例
public static void main(String[] args) throws IOException {
ArrayList strings = new ArrayList<>();
String http = “xx”;
HttpClient client = new DefaultHttpClient();
if (http.startsWith(“https”)) {
client = wrapClient(client);
}
HttpGet httpGet = new HttpGet(http);
HttpResponse execute = client.execute(httpGet);
String result = EntityUtils.toString(execute.getEntity());
JSONObject jsonObject = JSONObject.parseObject(result);
JSONArray jsonArray = jsonObject.getJSONObject(“result”).getJSONArray(“dataResults”);
for(int j=0;j<jsonArray.size();j++) {
String id = (String) jsonArray.getJSONObject(j).get(“id”);
System.out.println(id);
strings.add(id);
}
ArrayList<HashMap<String, String>> hashMaps = new ArrayList<>();
for (int runtime = 0; runtime < strings.size(); runtime++) {
HashMap<String, String> stringStringHashMap = new HashMap<>();
HashMap<String, Object> headerMap = new HashMap<>();
headerMap.put("id",strings.get(runtime));
HttpClient clientPost = new DefaultHttpClient();
clientPost = wrapClient(clientPost);
HttpPost post = new HttpPost("https://cpata-cabamc.portalca.thentiacloud.net/rest/public/custom-public-register/profile/individual/");
post.setHeader("Content-Type", "application/json");
StringEntity stringEntity = new StringEntity(JSON.toJSONString(headerMap), "utf-8");
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post.setEntity(stringEntity);
HttpResponse httpResponse = clientPost.execute(post);
String resultPost = EntityUtils.toString(httpResponse.getEntity());
JSONArray jsonArray1 = JSONObject.parseObject(resultPost).getJSONObject("result").getJSONArray("nameValuePairs");
for(int j=0;j<jsonArray1.size();j++) {
JSONObject jsonObject1 = jsonArray1.getJSONObject(j);
}
hashMaps.add(stringStringHashMap);
}
System.out.println(JSON.toJSONString(hashMaps));
}
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs,String string) {
}
public void checkServerTrusted(X509Certificate[] xcs,String string) {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}