-
-
Save hateum/8098653 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.net.SSLCertificateSocketFactory; | |
import android.net.SSLSessionCache; | |
import android.os.Build; | |
import org.apache.http.conn.ssl.SSLSocketFactory; | |
import org.apache.http.params.HttpParams; | |
import javax.net.ssl.SSLContext; | |
import javax.net.ssl.SSLSocket; | |
import javax.net.ssl.TrustManager; | |
import java.io.IOException; | |
import java.net.InetAddress; | |
import java.net.Socket; | |
import java.security.KeyManagementException; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.UnrecoverableKeyException; | |
public class MySocketFactory extends SSLSocketFactory { | |
private final javax.net.ssl.SSLSocketFactory mSocketFactory; | |
public MySocketFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { | |
super(null); | |
setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); | |
TrustManager[] trustManagers = {new MyTrustManager()}; | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) { | |
SSLSessionCache sslSessionCache = new SSLSessionCache(MyApp.get()); | |
SSLCertificateSocketFactory socketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0, sslSessionCache); | |
socketFactory.setTrustManagers(trustManagers); | |
mSocketFactory = socketFactory; | |
} else { | |
SSLContext sslContext = SSLContext.getInstance(SSLSocketFactory.TLS); | |
sslContext.init(null, trustManagers, null); | |
mSocketFactory = sslContext.getSocketFactory(); | |
} | |
} | |
@Override | |
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException { | |
SSLSocket sslSocket = (SSLSocket) mSocketFactory.createSocket(socket, host, port, autoClose); | |
getHostnameVerifier().verify(host, sslSocket); | |
return sslSocket; | |
} | |
@Override | |
public Socket createSocket() throws IOException { | |
return mSocketFactory.createSocket(); | |
} | |
@Override | |
public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params) throws IOException { | |
throw new UnsupportedOperationException(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment