package com.ljsd.util; 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.HttpPost; 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; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; public class HttpUtils { public static String doPost(String url, Map parms) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); List nvps =getFromMap(parms); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); CloseableHttpResponse response2 = httpclient.execute(httpPost); try { HttpEntity entity = response2.getEntity(); return EntityUtils.toString(entity); } catch (IOException e) { e.printStackTrace(); return null; } finally { response2.close(); } } public static List getFromMap(Map parms){ List nvps = new ArrayList(parms.size()); for(Map.Entry item : parms.entrySet()){ nvps.add(new BasicNameValuePair(item.getKey(), item.getValue())); } return nvps; } }