Created
August 21, 2015 04:48
-
-
Save kand617/8f13297c2808ef36f8b0 to your computer and use it in GitHub Desktop.
A programatic workaround using unirest for SSL validation exceptions
This file contains 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 com.mashape.unirest.http.Unirest; | |
import io.apimatic.controllers.TemplatesController; | |
import io.apimatic.examples.controllers.SimpleCalculatorController; | |
import io.apimatic.examples.http.client.APICallBack; | |
import io.apimatic.examples.models.OperationTypeEnum; | |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
import org.apache.http.conn.ssl.SSLContexts; | |
import org.apache.http.conn.ssl.TrustStrategy; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; | |
import org.apache.http.impl.nio.client.HttpAsyncClients; | |
import org.apache.log4j.LogManager; | |
import org.apache.log4j.Logger; | |
import org.apache.log4j.spi.LoggerRepository; | |
import javax.net.ssl.*; | |
import javax.security.cert.CertificateException; | |
import javax.security.cert.X509Certificate; | |
import java.security.KeyManagementException; | |
import java.security.NoSuchAlgorithmException; | |
/** | |
* Created by Kartik on 20/08/2015. | |
*/ | |
public class Program { | |
public static void main(String[] args) { | |
TrustStrategy acceptingTrustStrategy = new TrustStrategy() { | |
@Override | |
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { | |
return true; | |
} | |
public boolean isTrusted(X509Certificate[] certificate, String authType) { | |
return true; | |
} | |
}; | |
try { | |
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); | |
CloseableHttpClient client = HttpClients.custom() .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) | |
.setSslcontext(sslContext).build(); | |
Unirest.setHttpClient(client); | |
} catch (Exception e) { | |
// Handle error | |
} | |
SimpleCalculatorController calculatorController = new SimpleCalculatorController(); | |
calculatorController.getCalculateAsync(OperationTypeEnum.DIVIDE, 5, 5, new APICallBack<String>() { | |
@Override | |
public void onSuccess(Object o, String s) { | |
System.out.println("Success"); | |
} | |
@Override | |
public void onFailure(Object o, Throwable throwable) { | |
System.out.println("Failure"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment