Skip to content

Instantly share code, notes, and snippets.

@cbweixin
Created July 24, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cbweixin/3f8d79431a0e3d72df2b to your computer and use it in GitHub Desktop.
Save cbweixin/3f8d79431a0e3d72df2b to your computer and use it in GitHub Desktop.
by using org.apache.httpclient, how you could accept all certification, it is dangerous, but good for testing purpose.
package com.intuit.sb.dispatcher;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Created by xwei on 7/22/15.
*/
public class SimpleHttpClient {
public static String CONTENT_TYPE_HEADER_KEY = "Content-type";
public static String CONTENT_TYPE_HEADER_VALUE = "application/json; charset=UTF-8";
public static String ACCEPT_TYPE_HEADER_KEY = "Accept";
public static String ACCEPT_TYPE_HEADER_VALUE = "application/json; charset=UTF-8";
/* public static String CONTENT_TYPE_HEADER_KEY = "Content-type";
public static String CONTENT_TYPE_HEADER_VALUE = "text/plain; charset=UTF-8";
public static String ACCEPT_TYPE_HEADER_KEY = "Accept";
public static String ACCEPT_TYPE_HEADER_VALUE = "text/plain; charset=UTF-8";*/
public static void main(String[] args) throws IOException {
CloseableHttpResponse response = SimpleHttpClient.getHttp
("https://ci-develop-dispatcher-service.sbfs-acct-sbg-dispatcher-dev.a.intuit" +
".com/webapp/v1/dispatcher/realm/11111111/qbo");
System.out.println(convertStreamToString(response.getEntity().getContent()));
System.out.println(response.getStatusLine().getStatusCode());
}
private static String convertStreamToString(InputStream is) {
try {
return new Scanner(is).useDelimiter("\\A").next();
} catch (NoSuchElementException e) {
}
return "";
}
/**
* @param url
* @return
*/
public static CloseableHttpResponse getHttp(String url/*, String authHeader*/) {
HttpGet httpGet = new HttpGet(url);
//addHeaders(httpGet, authHeader);
CloseableHttpResponse httpResponse = null;
CloseableHttpClient httpClient = getHttpClient();
try {
httpResponse = httpClient.execute(httpGet);
} catch (Exception e) {
e.printStackTrace();
}
return httpResponse;
}
/**
* @return
*/
public static CloseableHttpClient getHttpClient() {
CloseableHttpClient httpClient;
try {
SSLContextBuilder builder = new SSLContextBuilder();
// trust the self signed cert
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
// trust any hostname by using NoopHostnameVerfier, then you don't need to implement a HostnameVerifier
// for trust anything, just re-use this "Noop" stuff
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build(), new NoopHostnameVerifier());
RequestConfig requestConfig =
RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build();
httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf).build();
return httpClient;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
/**
* @param httpGet
* @return
*/
public static HttpGet addHeaders(HttpGet httpGet, String authHeader) {
//String authHeader = ICNUtils.constructIAMAuthHeader();
// httpGet.addHeader(ICNConstants.IAM_AUTHORIZATION_HEADER, authHeader);
httpGet.addHeader(CONTENT_TYPE_HEADER_KEY, CONTENT_TYPE_HEADER_VALUE);
httpGet.addHeader(ACCEPT_TYPE_HEADER_KEY, ACCEPT_TYPE_HEADER_VALUE);
return httpGet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment