1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
public class MySSLSocketFactory implements ProtocolSocketFactory
{
private SSLContext sslcontext = null;
public MySSLSocketFactory() {
}
private static SSLContext createEasySSLContext()
{
try {
SSLContext context = SSLContext.getInstance("SSL");
context.init(null,new TrustManager[]{new MyX509TrustManager()},null);
return context;
} catch (Exception e) {
throw new HttpClientError(e.toString());
}
}
private SSLContext getSSLContext()
{
if (this.sslcontext == null)
this.sslcontext = createEasySSLContext();
return this.sslcontext;
}
public Socket createSocket( String host, int port, InetAddress clientHost, int clientPort)
throws IOException, UnknownHostException
{
return getSSLContext().getSocketFactory().createSocket(host, port, clientHost, clientPort);
}
public Socket createSocket(String host,int port,InetAddress localAddress,
int localPort, HttpConnectionParams params)
throws IOException, UnknownHostException
{
return createSocket(host, port, localAddress, localPort);
}
public Socket createSocket(String host, int port)
throws IOException, UnknownHostException
{
return getSSLContext().getSocketFactory().createSocket(host,port);
}
public boolean equals(Object obj) {
return ((obj != null) && obj.getClass().equals(MySSLSocketFactory.class));
}
public int hashCode() {
return MySSLSocketFactory.class.hashCode();
}
public static class MyX509TrustManager implements X509TrustManager
{
public MyX509TrustManager() {}
public X509Certificate[] getAcceptedIssuers() {return null;}
public void checkClientTrusted(X509Certificate[] chain, String authType){}
public void checkServerTrusted(X509Certificate[] chain, String authType){}
}
} |
Partager