Created
July 20, 2011 22:08
-
-
Save thiagocaiubi/1096047 to your computer and use it in GitHub Desktop.
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 java.io.IOException; | |
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 org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.protocol.ClientContext; | |
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.BasicCookieStore; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.protocol.BasicHttpContext; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.util.EntityUtils; | |
public class ApacheClient { | |
public static void main(String[] args) { | |
String url = "https://esitef-homologacao.softwareexpress.com.br/e-sitef/iniciarTransacao.se"; | |
try { | |
SSLContext ctx = SSLContext.getInstance("TLS"); | |
X509TrustManager tm = 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; | |
} | |
}; | |
ctx.init(null, new TrustManager[]{tm}, null); | |
SSLSocketFactory ssf = new SSLSocketFactory(ctx); | |
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); | |
HttpClient base = new DefaultHttpClient(); | |
ClientConnectionManager ccm = base.getConnectionManager(); | |
SchemeRegistry sr = ccm.getSchemeRegistry(); | |
sr.register(new Scheme("https", ssf, 443)); | |
HttpContext context = new BasicHttpContext(); | |
context.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore()); | |
HttpResponse response = base.execute(new HttpGet(url), context); | |
HttpEntity entity = response.getEntity(); | |
if (entity == null) { | |
System.out.println("Empty"); | |
} | |
try { | |
long len = entity.getContentLength(); | |
if (len < 10 * 1024 * 1024) { | |
System.out.println(EntityUtils.toString(entity)); | |
} else { | |
System.out.println("Empty"); | |
} | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment