java发起post请求—— body有参/无参
import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;
1 /** 2 * 发起post请求 有参数的情况 3 * @param url 4 * @param params 5 * @param codePage 6 * @return 7 * @throws Exception 8 */ 9 private synchronized static String postData(String url, Map<String, Object> params, String codePage) throws Exception { 10 //创建post请求对象 11 HttpPost httppost = new HttpPost(url); 12 // 获取到httpclient客户端 13 CloseableHttpClient httpclient = HttpClients.createDefault(); 14 try { 15 //创建参数集合 16 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); 17 //添加请求头参数 18 if(url.equals(GetOlapDataUrl)) { 19 httppost.addHeader("Content-Type", "application/json"); 20 httppost.addHeader("accessToken",params.get("accessToken").toString()); 21 } 22 // 设置请求的一些配置设置,主要设置请求超时,连接超时等参数 23 RequestConfig requestConfig = RequestConfig.custom() 24 .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) 25 .build(); 26 httppost.setConfig(requestConfig); 27 //添加参数 28 httppost.setEntity(new StringEntity(JSONObject.toJSONString(params), ContentType.create("application/json", "utf-8"))); 29 // 请求结果 30 String resultString = ""; 31 //启动执行请求,并获得返回值 32 CloseableHttpResponse response = httpclient.execute(httppost); 33 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 34 // 获取请求响应结果 35 HttpEntity entity = response.getEntity(); 36 if (entity != null) { 37 // 将响应内容转换为指定编码的字符串 38 resultString = EntityUtils.toString(entity, "UTF-8"); 39 System.out.printf("Response content:{}", resultString); 40 return resultString; 41 } 42 } else { 43 System.out.println("请求失败!"); 44 return resultString; 45 } 46 } catch (Exception e) { 47 throw e; 48 } finally { 49 httpclient.close(); 50 } 51 return null; 52 } 53 54 /** 55 * 发起post请求 没有任何参数的情况 56 * 57 * @param url 请求的目标url地址 58 * @param otherParam 其他参数 59 * @param data 请求数据 60 * @return 将响应结果转换成string返回 61 * @throws IOException 可能出现的异常 62 */ 63 private static String postMsg(String url, String otherParam) throws IOException { 64 // 根据url地址发起post请求 65 HttpPost httppost = new HttpPost(url); 66 // 获取到httpclient客户端 67 CloseableHttpClient httpclient = HttpClients.createDefault(); 68 try { 69 // 设置请求的一些头部信息 70 httppost.addHeader("Content-Type", "application/json"); 71 httppost.addHeader("accessToken", otherParam); 72 // 设置请求的一些配置设置,主要设置请求超时,连接超时等参数 73 RequestConfig requestConfig = RequestConfig.custom() 74 .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) 75 .build(); 76 httppost.setConfig(requestConfig); 77 // 执行请求 78 CloseableHttpResponse response = httpclient.execute(httppost); 79 // 请求结果 80 String resultString = ""; 81 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 82 // 获取请求响应结果 83 HttpEntity entity = response.getEntity(); 84 if (entity != null) { 85 // 将响应内容转换为指定编码的字符串 86 resultString = EntityUtils.toString(entity, "UTF-8"); 87 System.out.printf("Response content:{}", resultString); 88 return resultString; 89 } 90 } else { 91 System.out.println("请求失败!"); 92 return resultString; 93 } 94 } catch (Exception e) { 95 throw e; 96 } finally { 97 httpclient.close(); 98 } 99 return null; 100 }
/** * 发起post请求 有参数的情况 * @param url * @param params * @param codePage * @return * @throws Exception */ private synchronized static String postData(String url, Map<String, Object> params, String codePage) throws Exception { //创建post请求对象 HttpPost httppost = new HttpPost(url); // 获取到httpclient客户端 CloseableHttpClient httpclient = HttpClients.createDefault(); try { //创建参数集合 List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>(); //添加请求头参数 if(url.equals(GetOlapDataUrl)) { httppost.addHeader("Content-Type", "application/json"); httppost.addHeader("accessToken",params.get("accessToken").toString()); } // 设置请求的一些配置设置,主要设置请求超时,连接超时等参数 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) .build(); httppost.setConfig(requestConfig); //添加参数 httppost.setEntity(new StringEntity(JSONObject.toJSONString(params), ContentType.create("application/json", "utf-8"))); // 请求结果 String resultString = ""; //启动执行请求,并获得返回值 CloseableHttpResponse response = httpclient.execute(httppost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 获取请求响应结果 HttpEntity entity = response.getEntity(); if (entity != null) { // 将响应内容转换为指定编码的字符串 resultString = EntityUtils.toString(entity, "UTF-8"); System.out.printf("Response content:{}", resultString); return resultString; } } else { System.out.println("请求失败!"); return resultString; } } catch (Exception e) { throw e; } finally { httpclient.close(); } return null; }/** * 发起post请求 没有任何参数的情况 * * @param url 请求的目标url地址 * @param otherParam 其他参数 * @param data 请求数据 * @return 将响应结果转换成string返回 * @throws IOException 可能出现的异常 */ private static String postMsg(String url, String otherParam) throws IOException { // 根据url地址发起post请求 HttpPost httppost = new HttpPost(url); // 获取到httpclient客户端 CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 设置请求的一些头部信息 httppost.addHeader("Content-Type", "application/json"); httppost.addHeader("accessToken", otherParam); // 设置请求的一些配置设置,主要设置请求超时,连接超时等参数 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000) .build(); httppost.setConfig(requestConfig); // 执行请求 CloseableHttpResponse response = httpclient.execute(httppost); // 请求结果 String resultString = ""; if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 获取请求响应结果 HttpEntity entity = response.getEntity(); if (entity != null) { // 将响应内容转换为指定编码的字符串 resultString = EntityUtils.toString(entity, "UTF-8"); System.out.printf("Response content:{}", resultString); return resultString; } } else { System.out.println("请求失败!"); return resultString; } } catch (Exception e) { throw e; } finally { httpclient.close(); } return null; }