Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Central Credential Provider
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.SSLSocket;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.conn.ssl.X509HostnameVerifier;
import java.io.IOException;
import javax.net.ssl.*;
public class ClientWrapper {
private static final String TLS_INSTANCE = "TLS";
private static final String HTTPS_PROTOCOL = "https";
private static final int HTTPS_PORT = 443;
public static HttpClient wrapClient(HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance(TLS_INSTANCE);
X509TrustManager tm = initialiseTrustManager();
X509HostnameVerifier verifier = initialiseHostNameVerifier();
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(verifier);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme(HTTPS_PROTOCOL, ssf, HTTPS_PORT));
return new DefaultHttpClient(ccm, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
private static X509TrustManager initialiseTrustManager(){
return new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
}
private static X509HostnameVerifier initialiseHostNameVerifier(){
return new X509HostnameVerifier() {
@Override
public void verify(String string, SSLSocket ssls)
throws IOException {
}
@Override
public void verify(String string, X509Certificate xc)
throws SSLException {
}
@Override
public void verify(String string, String[] strings, String[] strings1)
throws SSLException {
}
@Override
public boolean verify(String string, SSLSession ssls) {
return true;
}
};
}
}
import java.io.IOException;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.net.URLEncoder;
public class SchedulerMain {
private static final String UTF_ENCODE = "UTF-8";
public static void main(String args[])
throws InterruptedException, ClientProtocolException, IOException,Exception {
HttpClient client = new DefaultHttpClient();
try
{
String appID=URLEncoder.encode("Backup-APP",UTF_ENCODE);
String safe=URLEncoder.encode("Backup",UTF_ENCODE);
String account=URLEncoder.encode("svc_backup",UTF_ENCODE);
String queryURL="https://compsrv01.corpad.loc/AIMWebService/api/Accounts?AppID="+
appID+"&Safe="+safe+"&UserName="+account;
HttpGet getRequest = new HttpGet(queryURL);
System.out.println("Executing request to " + getRequest.getURI());
client = ClientWrapper.wrapClient(client);
System.out.println("----------------------------------------");
HttpResponse httpResponse = client.execute(getRequest);
System.out.println("----------------------------------------");
HttpEntity entity = httpResponse.getEntity();
System.out.println(httpResponse.getStatusLine());
Header[] headers = httpResponse.getAllHeaders();
for (int i = 0; i < headers.length; i++)
System.out.println(headers[i]);
System.out.println("----------------------------------------");
if (entity != null)
System.out.println(EntityUtils.toString(entity));
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
}
}
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.