<a name='ClientUtil.java'></a> ClientUtil.java
1import org.apache.http.HttpEntity; 2import org.apache.http.HttpResponse; 3import org.apache.http.NameValuePair; 4import org.apache.http.client.HttpClient; 5import org.apache.http.client.entity.UrlEncodedFormEntity; 6import org.apache.http.client.methods.HttpPost; 7import org.apache.http.message.BasicNameValuePair; 8import org.apache.http.util.EntityUtils; 9import org.springframework.stereotype.Component; 10 11import java.util.ArrayList; 12import java.util.Iterator; 13import java.util.List; 14import java.util.Map; 15import java.util.Map.Entry; 16 17public class ClientUtil { 18 private String charset = "utf-8"; 19 public String doPost(String url, Map<String,String> map){ 20 HttpClient httpClient = null; 21 HttpPost httpPost = null; 22 String result = null; 23 try{ 24 httpClient = new SSLClient(); 25 httpPost = new HttpPost(url); 26 //设置参数 27 List<NameValuePair> list = new ArrayList<NameValuePair>(); 28 Iterator iterator = map.entrySet().iterator(); 29 while(iterator.hasNext()){ 30 Entry<String,String> elem = (Entry<String, String>) iterator.next(); 31 list.add(new BasicNameValuePair(elem.getKey(),elem.getValue())); 32 } 33 if(list.size() > 0){ 34 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset); 35 httpPost.setEntity(entity); 36 } 37 HttpResponse response = httpClient.execute(httpPost); 38 if(response != null){ 39 HttpEntity resEntity = response.getEntity(); 40 if(resEntity != null){ 41 result = EntityUtils.toString(resEntity,charset); 42 } 43 } 44 }catch(Exception ex){ 45 ex.printStackTrace(); 46 } 47 return result; 48 } 49} 50
<a name='SSLClient.java'></a> SSLClient.java
1import org.apache.http.conn.ClientConnectionManager; 2import org.apache.http.conn.scheme.Scheme; 3import org.apache.http.conn.scheme.SchemeRegistry; 4import org.apache.http.conn.ssl.SSLSocketFactory; 5import org.apache.http.impl.client.DefaultHttpClient; 6 7import javax.net.ssl.SSLContext; 8import javax.net.ssl.TrustManager; 9import javax.net.ssl.X509TrustManager; 10import java.security.cert.CertificateException; 11import java.security.cert.X509Certificate; 12 13//用于进行Https请求的HttpClient 14public class SSLClient extends DefaultHttpClient{ 15 public SSLClient() throws Exception{ 16 super(); 17 SSLContext ctx = SSLContext.getInstance("TLS"); 18 X509TrustManager tm = new X509TrustManager() { 19 @Override 20 public void checkClientTrusted(X509Certificate[] chain, 21 String authType) throws CertificateException { 22 } 23 @Override 24 public void checkServerTrusted(X509Certificate[] chain, 25 String authType) throws CertificateException { 26 } 27 @Override 28 public X509Certificate[] getAcceptedIssuers() { 29 return null; 30 } 31 }; 32 ctx.init(null, new TrustManager[]{tm}, null); 33 SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 34 ClientConnectionManager ccm = this.getConnectionManager(); 35 SchemeRegistry sr = ccm.getSchemeRegistry(); 36 sr.register(new Scheme("https", 443, ssf)); 37 } 38}