1. POST request
1.HTTP tool class
import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; 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.HttpUriRequest; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; 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.ssl.SSLContextBuilder; import org.apache.http.util.EntityUtils; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * HTTP client tool class (supports HTTPS) */ public class HttpClientUtils { \t /** * http get request * @param url */ public static String get(String url) { return get(url, "UTF-8"); } \t /** * http get request * @param url */ public static String get(String url, String charset) { HttpGet httpGet = new HttpGet(url); return executeRequest(httpGet, charset); } /** * http get request * @param url */ public static String get(String url, Map<String, String> headerMap) { return get(url, headerMap, "UTF-8"); } /** * http get request * @param url */ public static String get(String url, Map<String, String> headerMap, String charset) { HttpGet httpGet = new HttpGet(url); if(headerMap != null) { for (Map.Entry<String, String> entry : headerMap.entrySet()) { httpGet.setHeader(entry.getKey(), entry.getValue()); } } return executeRequest(httpGet, charset); } \t /** * HTTP get request, add asynchronous request header parameters * @param url */ public static String ajaxGet(String url) { return ajaxGet(url, "UTF-8"); } \t /** * HTTP get request, add asynchronous request header parameters * @param url */ public static String ajaxGet(String url, String charset) { HttpGet httpGet = new HttpGet(url); httpGet.setHeader("X-Requested-With", "XMLHttpRequest"); return executeRequest(httpGet, charset); } /** * HTTP post request, passing map format parameters */ public static String post(String url, Map<String, String> dataMap) { return post(url, dataMap, "UTF-8"); } /** * HTTP post request, passing map format parameters */ public static String post(String url, Map<String, String> dataMap, String charset) { HttpPost httpPost = new HttpPost(url); try { if (dataMap != null){ List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : dataMap.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset); formEntity.setContentEncoding(charset); httpPost.setEntity(formEntity); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return executeRequest(httpPost, charset); } /** * HTTP post request, passing map format parameters */ public static String post(String url, Map<String, String> dataMap, Map<String, String> headerMap) { return post(url, dataMap, headerMap, "UTF-8"); } /** * HTTP post request, passing map format parameters */ public static String post(String url, Map<String, String> dataMap, Map<String, String> headerMap, String charset) { HttpPost httpPost = new HttpPost(url); try { if(headerMap != null) { for (Map.Entry<String, String> entry : headerMap.entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue()); } } if (dataMap != null){ List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : dataMap.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset); formEntity.setContentEncoding(charset); httpPost.setEntity(formEntity); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return executeRequest(httpPost, charset); } /** * http post request, add asynchronous request header parameters and pass map format parameters */ public static String ajaxPost(String url, Map<String, String> dataMap) { return ajaxPost(url, dataMap, "UTF-8"); } /** * http post request, add asynchronous request header parameters and pass map format parameters */ public static String ajaxPost(String url, Map<String, String> dataMap, String charset) { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("X-Requested-With", "XMLHttpRequest"); try { if (dataMap != null){ List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : dataMap.entrySet()) { nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nvps, charset); formEntity.setContentEncoding(charset); httpPost.setEntity(formEntity); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return executeRequest(httpPost, charset); } /** * http post request, add asynchronous request header parameters and pass json format parameters */ public static String ajaxPostJson(String url, String jsonString) { return ajaxPostJson(url, jsonString, "UTF-8"); } /** * http post request, add asynchronous request header parameters and pass json format parameters */ public static String ajaxPostJson(String url, String jsonString, String charset) { HttpPost httpPost = new HttpPost(url); httpPost.setHeader("X-Requested-With", "XMLHttpRequest"); // try { StringEntity stringEntity = new StringEntity(jsonString, charset); // Solve the problem of Chinese garbled characters stringEntity.setContentEncoding(charset); stringEntity.setContentType("application/json"); httpPost.setEntity(stringEntity); // } catch (UnsupportedEncodingException e) { // e.printStackTrace(); // } return executeRequest(httpPost, charset); } /** * Execute an http request, passing HttpGet or HttpPost parameters */ public static String executeRequest(HttpUriRequest httpRequest) { return executeRequest(httpRequest, "UTF-8"); } /** * Execute an http request, passing HttpGet or HttpPost parameters */ public static String executeRequest(HttpUriRequest httpRequest, String charset) { CloseableHttpClient httpclient; if ("https".equals(httpRequest.getURI().getScheme())){ httpclient = createSSLInsecureClient(); }else{ httpclient = HttpClients.createDefault(); } String result = ""; try { try { CloseableHttpResponse response = httpclient.execute(httpRequest); HttpEntity entity = null; try { entity = response.getEntity(); result = EntityUtils.toString(entity, charset); } finally { EntityUtils.consume(entity); response.close(); } } finally { httpclient.close(); } }catch(IOException ex){ ex.printStackTrace(); } return result; } \t /** * Create SSL connection */ public static CloseableHttpClient createSSLInsecureClient() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (GeneralSecurityException ex) { throw new RuntimeException(ex); } } \t }
2.POST interface example:
http://IP address:8080/eUrbanMIS/openapi/v2/upstream?actionType=*** & amp;senderCode=*** & amp;data={***}
//The method here is called in specific business private String accReport(formal parameter) { //The passed parameters encapsulate the object according to the business AccReportVO searchMapJsonBean=new AccReportVO(); //Part of the business code is omitted here String parmStr =JSON.toJSONString(searchMapJsonBean); Map<String, String> map = new HashMap<>(); map.put("actionType", "***"); map.put("senderCode", "***"); map.put("data", parmStr); //misUrl is the interface request address, the example here is post request String res= HttpClientUtils.post(misUrl,map); log.info("return result" + res); return res; }
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Network Skill TreeHomepageOverview 42372 people are learning the system