android https通過載入pfx認證擷取資料

來源:互聯網
上載者:User

直接給代碼吧,研究了幾天才搞定......

public static final String CLIENT_KET_PASSWORD = "Ku6OpqKDfN4=305790"; //public static String getNewHttpClient(String url){try{// KeyStore trustStore = KeyStore.getInstance("PKCS12", "BC");// trustStore// .load(PcPostApplication.getInstance().getAssets()// .open("abc.pfx"), CLIENT_KET_PASSWORD.toCharArray());SSLSocketFactory sf =new SSLSocketFactoryEx(AppConfig.mKeyStore,AppConfig.CERTFILE_PASSWORD.toCharArray());sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);HttpParams params = new BasicHttpParams();HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);SchemeRegistry registry = new SchemeRegistry();registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));registry.register(new Scheme("https", sf, 443));HttpClient client = null;String msg = "";try{ClientConnectionManager ccm =new ThreadSafeClientConnManager(params, registry);client = new DefaultHttpClient(ccm, params);HttpGet hg = new HttpGet(url);HttpResponse response = client.execute(hg);HttpEntity entity = response.getEntity();if (entity != null){InputStream instreams = entity.getContent();msg = convertStreamToString(instreams);}return msg;}catch (Exception e){// TODO Auto-generated catch blocke.printStackTrace();}}catch (Exception e){e.printStackTrace();}return "";}public static String convertStreamToString(InputStream is){BufferedReader reader = new BufferedReader(new InputStreamReader(is));StringBuilder sb = new StringBuilder();String line = "";try{while ((line = reader.readLine()) != null){sb.append(line + "\n");}}catch (IOException e){e.printStackTrace();}finally{try{is.close();}catch (IOException e){e.printStackTrace();}}return sb.toString();}
上面的載入認證並請求,但是在這需要注意一個問題SSLSocketFactory需要自訂,看下面自訂的代碼:


public class SSLSocketFactoryEx extends SSLSocketFactory{SSLContext sslContext = SSLContext.getInstance("TLS");public SSLSocketFactoryEx(KeyStore truststore, char[] arry)throws NoSuchAlgorithmException, KeyManagementException,KeyStoreException, UnrecoverableKeyException{super(truststore);KeyManagerFactory localKeyManagerFactory =KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());localKeyManagerFactory.init(truststore, arry);KeyManager[] arrayOfKeyManager =localKeyManagerFactory.getKeyManagers();TrustManager tm = new X509TrustManager(){@Overridepublic X509Certificate[] getAcceptedIssuers(){return null;}@Overridepublic void checkServerTrusted(X509Certificate[] chain,String authType) throws CertificateException{}@Overridepublic void checkClientTrusted(X509Certificate[] chain,String authType) throws CertificateException{}};sslContext.init(arrayOfKeyManager, new TrustManager[] { tm },new java.security.SecureRandom());}@Overridepublic Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException{return sslContext.getSocketFactory().createSocket(socket, host, port,autoClose);}@Overridepublic Socket createSocket() throws IOException{return sslContext.getSocketFactory().createSocket();}}

因為是雙向握手,那個sslcontext不能初始化為空白.....問題解決了......

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.