HttpClient 提交https 请求(转)
import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.client.HttpClient; 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 org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; /** * 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”异常 * 不用导入SSL证书 * @author shipengzhi(shipengzhi@sogou-inc.com) * */ public class WebClientDevWrapper{ public static HttpClient wrapClient(HttpClient base) { try { SSLContext ctx = SSLContext.getInstance("TLS"); X509TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted( X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } @Override public void checkServerTrusted( X509Certificate[] chain, String authType) throws CertificateException { } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", 443, ssf)); ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry); return new DefaultHttpClient(mgr, base.getParams()); } catch (Exception ex) { ex.printStackTrace(); return null; } } }
import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; 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 org.apache.http.params.CoreConnectionPNames; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import com.sun.xml.internal.messaging.saaj.packaging.mime.Header; /** * 获取助手提供上的plist文件信息。 保存 创建本地plist文件 * 获取的信息包括 应用的 名称 版本号 ipa下载地址 bundleid * @author DLHT * */ public class ReadPlist { private Logger log = Logger.getLogger(this.getClass()); // 发送https 请求 public Map<String, Object> readplist92(String plisturl) { Map<String, Object> map = null; Document document; DocumentBuilderFactory documentBF = DocumentBuilderFactory .newInstance(); documentBF.setNamespaceAware(true); InputStream inStream = null; try { DocumentBuilder db = documentBF.newDocumentBuilder(); HttpClient httpclient = new DefaultHttpClient(); // Secure Protocol implementation. SSLContext ctx = SSLContext.getInstance("SSL"); // Implementation of a trust manager for X509 certificates X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx); ClientConnectionManager ccm = httpclient.getConnectionManager(); // register https protocol in httpclient's scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); HttpGet httpGet = new HttpGet(plisturl); httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000); HttpResponse response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { inStream = entity.getContent(); Header[] aHeaders = (Header[]) response.getAllHeaders(); for (Header header : aHeaders) { System.out.println(header); } document = db.parse(inStream); NodeList nl = document.getElementsByTagName("string"); String ipaurl = nl.item(1).getFirstChild().getNodeValue(); // ipa // 下载地址 String bundleid = nl.item(6).getFirstChild().getNodeValue(); // bundleid String version = nl.item(7).getFirstChild().getNodeValue(); // 版本号码 String title = nl.item(9).getFirstChild().getNodeValue(); // app名称 String icon = nl.item(3).getFirstChild().getNodeValue(); // icon // 图标 map = new HashMap<String, Object>(); map.put("bundleid", bundleid); map.put("version", version); map.put("appname", title); map.put("ipaurl", ipaurl); map.put("icon", icon); } } catch (Exception e) { log.error(e.getMessage()); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { log.error(e.getMessage()); } } } return map; } public Map<String, Object> readplist91(String plisturl) { Map<String, Object> map = null; Document document; DocumentBuilderFactory documentBF = DocumentBuilderFactory .newInstance(); documentBF.setNamespaceAware(true); InputStream inStream = null; try { DocumentBuilder db = documentBF.newDocumentBuilder(); HttpClient httpClient = new DefaultHttpClient(); httpClient = WebClientDevWrapper.wrapClient(httpClient); HttpGet httpGet = new HttpGet(plisturl); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { inStream = entity.getContent(); document = db.parse(inStream); NodeList nl = document.getElementsByTagName("string"); String ipaurl = nl.item(1).getFirstChild().getNodeValue(); // ipa // 下载地址 String bundleid = nl.item(6).getFirstChild().getNodeValue(); // bundleid String version = nl.item(7).getFirstChild().getNodeValue(); // 版本号码 String title = nl.item(9).getFirstChild().getNodeValue(); // app名称 String icon = nl.item(3).getFirstChild().getNodeValue(); // icon // 图标 map = new HashMap<String, Object>(); map.put("bundleid", bundleid); map.put("version", version); map.put("appname", title); map.put("ipaurl", ipaurl); map.put("icon", icon); } } catch (Exception e) { e.printStackTrace(); /* * log.error(e.getMessage()); System.out.println(e.getMessage()); */ } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { log.error(e.getMessage()); System.out.println(e.getMessage()); } } } return map; } // 发送 http 请求 public Map<String, Object> readplist(String plisturl) { Map<String, Object> map = null; Document document; DocumentBuilderFactory documentBF = DocumentBuilderFactory .newInstance(); documentBF.setNamespaceAware(true); InputStream inStream = null; try { DocumentBuilder db = documentBF.newDocumentBuilder(); URL url = new URL(plisturl); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); inStream = urlConnection.getInputStream(); document = db.parse(inStream); NodeList nl = document.getElementsByTagName("string"); String ipaurl = nl.item(1).getFirstChild().getNodeValue(); // ipa // 下载地址 String bundleid = nl.item(6).getFirstChild().getNodeValue(); // bundleid String version = nl.item(7).getFirstChild().getNodeValue(); // 版本号码 String title = nl.item(9).getFirstChild().getNodeValue(); // app名称 String icon = nl.item(3).getFirstChild().getNodeValue(); // icon 图标 map = new HashMap<String, Object>(); map.put("bundleid", bundleid); map.put("version", version); map.put("appname", title); map.put("ipaurl", ipaurl); map.put("icon", icon); } catch (Exception e) { log.error(e.getMessage()); System.out.println(e.getMessage()); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { log.error(e.getMessage()); System.out.println(e.getMessage()); } } } return map; } }