HTTP帮助类
package com.abc.helper; /** * Created by xxx. */ import com.abc.model.Enums; import org.apache.commons.codec.binary.Base64; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import java.nio.charset.StandardCharsets; public class HttpClientHelper { private String charset = "utf-8"; private Enums.ContentTypeEnum contentType = Enums.ContentTypeEnum.Json; //application/x-www-form-urlencoded //text/xml private String username = ""; private String password = ""; public String get(String url) { Object httpClient = null; HttpGet httpGet = null; String result = null; //判断是否以http或者https开头 String s = url.toLowerCase(); if (!s.startsWith("http")) url = "http://" + url; try { httpClient = url.toLowerCase().startsWith("https")?new SSLClient():new DefaultHttpClient(); httpGet = new HttpGet(url); HttpResponse response = ((DefaultHttpClient)httpClient).execute(httpGet); if(response != null) { HttpEntity resEntity = response.getEntity(); if(resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception var15) { var15.printStackTrace(); } return result; } public String post(String url, String json) { Object httpClient = null; HttpPost httpPost = null; String result = null; //判断是否以http或者https开头 String s = url.toLowerCase(); if (!s.startsWith("http")) url = "http://" + url; try { httpClient = url.toLowerCase().startsWith("https")?new SSLClient():new DefaultHttpClient(); httpPost = new HttpPost(url); if (this.username != null && this.username.length() > 0 && this.password != null && this.password.length() > 0) { String ex = username + ":" + password; byte[] encodedAuth = Base64.encodeBase64(ex.getBytes(StandardCharsets.UTF_8)); String authHeader = "Basic " + new String(encodedAuth); httpPost.setHeader("Authorization", authHeader); } StringEntity se = new StringEntity(json, this.charset); // "UTF-8"); se.setContentType(this.contentType.value); // "application/json"); httpPost.setEntity(se); HttpResponse response = ((DefaultHttpClient)httpClient).execute(httpPost); if(response != null) { HttpEntity resEntity = response.getEntity(); if(resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception var15) { var15.printStackTrace(); } return result; } public void setCharset(String charset) { this.charset = charset; } public void setAuthorization(String username, String password) { this.username = username; this.password = password; } public void setContentType(Enums.ContentTypeEnum contentType) { this.contentType = contentType; } }
package com.abc.helper; /** * Created by xxx. */ import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import javax.net.ssl.KeyManager; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class SSLClient extends DefaultHttpClient { public SSLClient() throws Exception { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init((KeyManager[])null, new TrustManager[]{tm}, (SecureRandom)null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); ClientConnectionManager ccm = this.getConnectionManager(); SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); } }
有些事情,没经历过不知道原理,没失败过不明白奥妙,没痛苦过不了解真谛。临渊羡鱼,不如退而结网!

浙公网安备 33010602011771号