How to execute a https GET request from java -
How to execute a https GET request from java -
i wrote java client executes http requests without problem. want modify client in order
import org.apache.http.httphost; import org.apache.http.auth.authscope; import org.apache.http.auth.usernamepasswordcredentials; import org.apache.http.client.credentialsprovider; 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.impl.client.basiccredentialsprovider; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; private string executeget(final string url, string proxy, int port) throws ioexception, requestunsuccesfulexception, invalidparameterexception { closeablehttpclient httpclient = null; string ret = ""; requestconfig config; seek { string hostname = extracthostname(url); logger.info("hostname {}", hostname); httphost target = new httphost(hostname, 80, null); httphost myproxy = new httphost(proxy, port, "http"); credentialsprovider credsprovider = new basiccredentialsprovider(); credsprovider.setcredentials( authscope.any, new usernamepasswordcredentials(username, password)); httpclient = httpclients.custom().setdefaultcredentialsprovider(credsprovider).build(); config = requestconfig.custom().setproxy(myproxy).build(); httpget request = new httpget(url); request.setconfig(config); closeablehttpresponse response = httpclient.execute(target, request); ...
now have send https
requests instead of https
. expecting easy modification httpsget
instead of httpget
not, there no httpsget
class available.
what easiest way modify method in order handle https
requests.
this quick , dirty https client in java, ignores invalid certificates , authenticates using basic
import java.io.ioexception; import java.net.url; import java.security.keymanagementexception; import java.security.nosuchalgorithmexception; import javax.net.ssl.httpsurlconnection; import javax.net.ssl.sslcontext; import javax.net.ssl.trustmanager; public static httpsurlconnection getconnection(boolean ignoreinvalidcertificate, string user, string pass, httprequestmethod httprequestmethod, url url) throws keymanagementexception, nosuchalgorithmexception, ioexception{ sslcontext ctx = sslcontext.getinstance("tls"); if (ignoreinvalidcertificate){ ctx.init(null, new trustmanager[] { new invalidcertificatetrustmanager() }, null); } sslcontext.setdefault(ctx); string authstr = user+":"+pass; string authencoded = base64.encodebytes(authstr.getbytes()); httpsurlconnection connection = (httpsurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setdooutput(true); connection.setrequestproperty("authorization", "basic " + authencoded); if (ignoreinvalidcertificate){ connection.sethostnameverifier(new invalidcertificatehostverifier()); } homecoming connection; }
--
import javax.net.ssl.hostnameverifier; import javax.net.ssl.sslsession; public class invalidcertificatehostverifier implements hostnameverifier{ @override public boolean verify(string paramstring, sslsession paramsslsession) { homecoming true; } }
--
import java.security.cert.certificateexception; import java.security.cert.x509certificate; import javax.net.ssl.x509trustmanager; /** * ignore invalid https certificate opam * <p>see http://javaskeleton.blogspot.com.br/2011/01/avoiding-sunsecurityvalidatorvalidatore.html */ public class invalidcertificatetrustmanager implements x509trustmanager{ @override public x509certificate[] getacceptedissuers() { homecoming null; } @override public void checkservertrusted(x509certificate[] paramarrayofx509certificate, string paramstring) throws certificateexception { } @override public void checkclienttrusted(x509certificate[] paramarrayofx509certificate, string paramstring) throws certificateexception { } }
maybe it's can start with.
of course, since have connection, can retrieve response contents using
inputstream content = (inputstream) connection.getinputstream();
java https get
Comments
Post a Comment