Java tutorial
/* * Copyright (C) 2012-2014 Japan Smartphone Security Association * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jssec.android.https.privatecertificate; import java.security.KeyStore; import javax.net.ssl.SSLException; import org.apache.http.HttpException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.content.Context; import android.os.AsyncTask; public abstract class PrivateCertificateHttpsGet extends AsyncTask<String, Void, Object> { private Context mContext; public PrivateCertificateHttpsGet(Context context) { mContext = context; } @Override protected Object doInBackground(String... params) { DefaultHttpClient client = new DefaultHttpClient(); try { // ?1 ??? // assets??????????KeyStoreclient? KeyStore ks = KeyStoreUtil.getEmptyKeyStore(); KeyStoreUtil.loadX509Certificate(ks, mContext.getResources().getAssets().open("cacert.crt")); Scheme sch = new Scheme("https", new SSLSocketFactory(ks), 443); client.getConnectionManager().getSchemeRegistry().register(sch); // ?2 URI?https://?? // ?3 ??????? HttpGet request = new HttpGet(params[0]); HttpResponse response = client.execute(request); checkResponse(response); // ?4 ????????? return EntityUtils.toByteArray(response.getEntity()); } catch (SSLException e) { // ?5 SSLException????????? // ?????? return e; } catch (Exception e) { return e; } finally { // ?HttpClientshutdown? client.getConnectionManager().shutdown(); } } private void checkResponse(HttpResponse response) throws HttpException { int statusCode = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK != statusCode) { throw new HttpException("HttpStatus: " + statusCode); } } }