use of javax.net.ssl.HttpsURLConnection in project cas by apereo.
the class ValidateCaptchaAction method doExecute.
@Override
protected Event doExecute(final RequestContext requestContext) throws Exception {
final HttpServletRequest request = WebUtils.getHttpServletRequest(requestContext);
final String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
if (StringUtils.isBlank(gRecaptchaResponse)) {
LOGGER.warn("Recaptcha response is missing from the request");
return getError(requestContext);
}
try {
final URL obj = new URL(recaptchaProperties.getVerifyUrl());
final HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", WebUtils.getHttpServletRequestUserAgent());
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
final String postParams = "secret=" + recaptchaProperties.getSecret() + "&response=" + gRecaptchaResponse;
LOGGER.debug("Sending 'POST' request to URL: [{}]", obj);
con.setDoOutput(true);
try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.writeBytes(postParams);
wr.flush();
}
final int responseCode = con.getResponseCode();
LOGGER.debug("Response Code: [{}]", responseCode);
if (responseCode == HttpStatus.OK.value()) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8))) {
final String response = in.lines().collect(Collectors.joining());
LOGGER.debug("Google captcha response received: [{}]", response);
final JsonNode node = READER.readTree(response);
if (node.has("success") && node.get("success").booleanValue()) {
return null;
}
}
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
return getError(requestContext);
}
use of javax.net.ssl.HttpsURLConnection in project UltimateAndroid by cymcsg.
the class HttpsUtils method sendWithSSlSocket.
/**
* @deprecated
*/
public static void sendWithSSlSocket(String urlLink) {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = null;
try {
url = new URL(urlLink);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(sslsocketfactory);
InputStream inputstream = conn.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
System.out.println("Received " + string);
}
} catch (Exception e) {
e.printStackTrace();
Logs.e(e, "");
}
}
use of javax.net.ssl.HttpsURLConnection in project dropwizard by dropwizard.
the class SslReloadAppTest method certBytes.
/** Issues a POST against the reload ssl admin task, asserts that the code and content
* are as expected, and finally returns the server certificate */
private byte[] certBytes(int code, String content) throws Exception {
final URL url = new URL("https://localhost:" + rule.getAdminPort() + "/tasks/reload-ssl");
final HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
try {
postIt(conn);
assertThat(conn.getResponseCode()).isEqualTo(code);
if (code == 200) {
assertThat(CharStreams.toString(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))).isEqualTo(content);
} else {
assertThat(CharStreams.toString(new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8))).contains(content);
}
// Thus, we return the one and only certificate.
return conn.getServerCertificates()[0].getEncoded();
} finally {
conn.disconnect();
}
}
use of javax.net.ssl.HttpsURLConnection in project AndroidNetworkDemo by dodocat.
the class SelfSignSslOkHttpStack method createConnection.
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
if ("https".equals(url.getProtocol()) && socketFactoryMap.containsKey(url.getHost())) {
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(okHttpClient).open(url);
connection.setSSLSocketFactory(socketFactoryMap.get(url.getHost()));
return connection;
} else {
return new OkUrlFactory(okHttpClient).open(url);
}
}
use of javax.net.ssl.HttpsURLConnection in project buck by facebook.
the class HttpDownloader method fetch.
@Override
public boolean fetch(BuckEventBus eventBus, URI uri, Optional<PasswordAuthentication> authentication, Path output) throws IOException {
if (!("https".equals(uri.getScheme()) || "http".equals(uri.getScheme()))) {
return false;
}
DownloadEvent.Started started = DownloadEvent.started(uri);
eventBus.post(started);
try {
HttpURLConnection connection = createConnection(uri);
if (authentication.isPresent()) {
if ("https".equals(uri.getScheme()) && connection instanceof HttpsURLConnection) {
PasswordAuthentication p = authentication.get();
String authStr = p.getUserName() + ":" + new String(p.getPassword());
String authEncoded = BaseEncoding.base64().encode(authStr.getBytes(StandardCharsets.UTF_8));
connection.addRequestProperty("Authorization", "Basic " + authEncoded);
} else {
LOG.info("Refusing to send basic authentication over plain http.");
return false;
}
}
if (HttpURLConnection.HTTP_OK != connection.getResponseCode()) {
LOG.info("Unable to download %s: %s", uri, connection.getResponseMessage());
return false;
}
long contentLength = connection.getContentLengthLong();
try (InputStream is = new BufferedInputStream(connection.getInputStream());
OutputStream os = new BufferedOutputStream(Files.newOutputStream(output))) {
long read = 0;
while (true) {
int r = is.read();
read++;
if (r == -1) {
break;
}
if (read % PROGRESS_REPORT_EVERY_N_BYTES == 0) {
eventBus.post(new DownloadProgressEvent(uri, contentLength, read));
}
os.write(r);
}
}
return true;
} finally {
eventBus.post(DownloadEvent.finished(started));
}
}
Aggregations