how to make HTTPS calls using AsyncHttpClient?

后端 未结 4 1002
太阳男子
太阳男子 2020-12-08 12:33

i was using AsyncHttpClient link for making http calls but now our server has migrated to HTTPS and I am getting exception javax.net.ssl.SSLPeerUnverified

4条回答
  •  甜味超标
    2020-12-08 13:18

    I have a simple http client that calls https service:

    import android.util.Log;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.AbstractHttpClient;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.protocol.HTTP;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;
    import java.util.List;
    
    /**
     */
    public class HttpSimpleClient {
    
        private static final String TAG = HttpSimpleClient.class.getSimpleName();
    
        private static HttpSimpleClient instance;
    
        private HttpSimpleClient(){}
    
        public static HttpSimpleClient instance(){
            if (instance==null) {
                instance = new HttpSimpleClient();
            }
            return instance;
        }
    
        public  T post(URL url,
                          List header,
                          List parameter,
                          ResponseHandler responseHandler) throws IOException {
            return post(url, header, parameter, responseHandler, null, null);
        }
    
        public  T post(URL url,
                         List header,
                         List parameter,
                         ResponseHandler responseHandler,
                         AuthScope proxy,
                         UsernamePasswordCredentials proxyUser) throws IOException {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost request = new HttpPost(url.toString());
    
            if (header!=null) {
                for (NameValuePair head : header){
                    request.setHeader(head.getName(), head.getValue());
                }
                Log.d(TAG, "Aggiunti header: "+ header.size());
    
            } else {
                // Header di default
                request.setHeader("Content-Type", "application/x-www-form-urlencoded");
                request.setHeader("User-Agent", System.getProperty("http.agent"));
                Log.d(TAG, "Aggiunti header di defautl");
            }
    
            if (parameter!=null) {
                request.setEntity(new UrlEncodedFormEntity(parameter, HTTP.UTF_8));
                Log.d(TAG, "Aggiunti parametri: "+ parameter.size());
            }
    
    
            if (proxy!=null) {
                if (proxyUser!=null) {
                    ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(proxy, proxyUser);
    
                } else {
                    // TODO gestire proxy senza credenziali
                    ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials(proxy, null);
    
                }
                Log.d(TAG, "Impostato Proxy per la connessione");
            }
            return httpClient.execute(request, responseHandler);
        }
    
    
        public static String httpResponseToString(HttpResponse httpResponse, int bufferSize) throws IOException {
            InputStream content = httpResponse.getEntity().getContent();
    
            int numRead;
            byte[] buffer = new byte[bufferSize];
            ByteArrayOutputStream outString = new ByteArrayOutputStream();
            try{
                while ((numRead = content.read(buffer)) != -1) {
                    outString.write(buffer, 0, numRead);
                }
            } finally {
                content.close();
            }
            return new String(outString.toByteArray());
        }
    }
    

    And i use it in a AsyncTask:

    response = HttpSimpleClient.instance().post(
                        getServiceURL(), // HTTPS url
                        mHeader, // List
                        mParameter, // List
                        getResponseHandler() // org.apache.http.client.ResponseHandler
                );
    

    Tested on Android api>=10

提交回复
热议问题