出处:https://www.cnblogs.com/jshtest/p/9070711.html
package com.ftms.didan.info.test; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.poi.ss.formula.functions.T; import org.jeecg.common.api.vo.Result; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.ftms.didan.info.entity.DdInfo; public class Test { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id", "1341637844113121282"); jsonObject.put("type", "history"); // jsonObject.put("password", "pdmi1234"); // String url = "http://192.168.1.161:8080/didan/info/ddInfo/list/queryPageDdInfoPublish?pageNo=1&pageSize=10"; // String url = "http://192.168.1.161:8080/didan/version/ddVersion/queryByVerstionType?type=android"; String url = "http://192.168.1.161:8080/didan/version/ddVersion/setVersion"; CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); // 配置超时时间 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(5000) .setConnectionRequestTimeout(5000) .setRedirectsEnabled(true) .build(); HttpPut httpPost = new HttpPut(url); //设置超时时间 httpPost.setConfig(requestConfig); httpPost.setHeader("Content-Type", "application/json"); httpPost.setHeader("X-Access-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDg3MjM1MDYsInVzZXJuYW1lIjoiZnRtc2FkbWluIn0.kAonFuVKswsrhAJI4VcrG9WPncYj6- WMmKsLQUW98II"); try { StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8"); entity.setContentEncoding("utf-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); //执行post CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost); String strRequest = ""; if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) { System.out.println(closeableHttpResponse.getStatusLine().getStatusCode()); if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity httpEntity = closeableHttpResponse.getEntity(); strRequest = EntityUtils.toString(httpEntity); System.out.println(strRequest); // 把字符串转换为json对象 // JSONObject json = JSON.parseObject(strRequest); // JSONObject result = json.getJSONObject("result"); // JSONArray arr = result.getJSONArray("records"); // List<DdInfo> errors = JSON.parseObject(arr.toJSONString(), new TypeReference<List<DdInfo>>() {}); // System.out.println(json); } else { strRequest = "Error Response" + closeableHttpResponse.getStatusLine().getStatusCode(); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (closeableHttpClient != null) { closeableHttpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } } }