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 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));
}
}
use of javax.net.ssl.HttpsURLConnection in project jersey by jersey.
the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.
/**
* Test to see that the correct Http status is returned.
*
* @throws Exception in case of a test failure.
*/
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
final SSLContext sslContext = getSslContext();
final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {
@Override
public HttpURLConnection getConnection(final URL url) throws IOException {
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(socketFactory);
return connection;
}
}));
final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
final Response response = client.target(Server.BASE_URI).path("/").request().get();
assertEquals(200, response.getStatus());
assertTrue(socketFactory.isVisited());
}
use of javax.net.ssl.HttpsURLConnection in project hadoop by apache.
the class Fetcher method openConnection.
@VisibleForTesting
protected synchronized void openConnection(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (sslShuffle) {
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
try {
httpsConn.setSSLSocketFactory(sslFactory.createSSLSocketFactory());
} catch (GeneralSecurityException ex) {
throw new IOException(ex);
}
httpsConn.setHostnameVerifier(sslFactory.getHostnameVerifier());
}
connection = conn;
}
use of javax.net.ssl.HttpsURLConnection in project hadoop by apache.
the class TimelineConnector method initSslConnConfigurator.
private static ConnectionConfigurator initSslConnConfigurator(final int timeout, SSLFactory sslFactory) throws IOException, GeneralSecurityException {
final SSLSocketFactory sf;
final HostnameVerifier hv;
sf = sslFactory.createSSLSocketFactory();
hv = sslFactory.getHostnameVerifier();
return new ConnectionConfigurator() {
@Override
public HttpURLConnection configure(HttpURLConnection conn) throws IOException {
if (conn instanceof HttpsURLConnection) {
HttpsURLConnection c = (HttpsURLConnection) conn;
c.setSSLSocketFactory(sf);
c.setHostnameVerifier(hv);
}
setTimeouts(conn, timeout);
return conn;
}
};
}
Aggregations