Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A programatic workaround using unirest for SSL validation exceptions
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
You can’t perform that action at this time.