Java Code Examples for javax.net.ssl.HttpsURLConnection#setHostnameVerifier()
The following examples show how to use
javax.net.ssl.HttpsURLConnection#setHostnameVerifier() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SslClientHttpRequestFactory.java From jsonrpc4j with MIT License | 6 votes |
@Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if (connection instanceof HttpsURLConnection) { final HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; if (hostNameVerifier != null) { httpsConnection.setHostnameVerifier(hostNameVerifier); } if (sslContext != null) { httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory()); } } super.prepareConnection(connection, httpMethod); }
Example 2
Source File: DefaultImageDownloader.java From RichText with MIT License | 6 votes |
@Override public InputStream getInputStream() throws IOException { URL url = new URL(this.url); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(10000); connection.setDoInput(true); connection.addRequestProperty("Connection", "Keep-Alive"); if (connection instanceof HttpsURLConnection) { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection; httpsURLConnection.setHostnameVerifier(DO_NOT_VERIFY); httpsURLConnection.setSSLSocketFactory(sslContext.getSocketFactory()); } connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == 200) { inputStream = connection.getInputStream(); return inputStream; } else { throw new HttpResponseCodeException(responseCode); } }
Example 3
Source File: Configs.java From datacollector with Apache License 2.0 | 6 votes |
@VisibleForTesting public HttpURLConnection createConnection(String hostPort, String path) throws IOException, StageException { String scheme = (tlsConfigBean.isEnabled()) ? "https://" : "http://"; URL url = new URL(scheme + hostPort.trim() + path); HttpURLConnection conn = createConnection(url); conn.setConnectTimeout(connectionTimeOutMs); conn.setReadTimeout(readTimeOutMs); if (tlsConfigBean.isEnabled()) { HttpsURLConnection sslConn = (HttpsURLConnection) conn; sslConn.setSSLSocketFactory(sslSocketFactory); if (!hostVerification) { sslConn.setHostnameVerifier(ACCEPT_ALL_HOSTNAME_VERIFIER); } } conn.setRequestProperty(Constants.X_SDC_APPLICATION_ID_HEADER, appId.get()); return conn; }
Example 4
Source File: HttpsSocketFacTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); uc.setSSLSocketFactory(sssf); uc.setHostnameVerifier(new AllHostnameVerifier()); InputStream is = uc.getInputStream(); byte[] ba = new byte[1024]; int read = 0; while ((read = is.read(ba)) != -1) { System.out.println(new String(ba, 0, read)); } System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); if (!sssf.socketCreated) throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); }
Example 5
Source File: HttpsSocketFacTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); uc.setSSLSocketFactory(sssf); uc.setHostnameVerifier(new AllHostnameVerifier()); InputStream is = uc.getInputStream(); byte[] ba = new byte[1024]; int read = 0; while ((read = is.read(ba)) != -1) { System.out.println(new String(ba, 0, read)); } System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); if (!sssf.socketCreated) throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); }
Example 6
Source File: ConnectionBuilderForTest.java From okta-sdk-appauth-android with Apache License 2.0 | 6 votes |
@NonNull @Override public HttpURLConnection openConnection(@NonNull Uri uri) throws IOException { Preconditions.checkNotNull(uri, "url must not be null"); Preconditions.checkArgument(HTTP.equals(uri.getScheme()) || HTTPS.equals(uri.getScheme()), "scheme or uri must be http or https"); HttpURLConnection conn = (HttpURLConnection) new URL(uri.toString()).openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT_MS); conn.setReadTimeout(READ_TIMEOUT_MS); conn.setInstanceFollowRedirects(false); if (conn instanceof HttpsURLConnection && TRUSTING_CONTEXT != null) { HttpsURLConnection httpsConn = (HttpsURLConnection) conn; httpsConn.setSSLSocketFactory(TRUSTING_CONTEXT.getSocketFactory()); httpsConn.setHostnameVerifier(ANY_HOSTNAME_VERIFIER); } return conn; }
Example 7
Source File: HttpsSocketFacTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); uc.setSSLSocketFactory(sssf); uc.setHostnameVerifier(new AllHostnameVerifier()); InputStream is = uc.getInputStream(); byte[] ba = new byte[1024]; int read = 0; while ((read = is.read(ba)) != -1) { System.out.println(new String(ba, 0, read)); } System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); if (!sssf.socketCreated) throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); }
Example 8
Source File: HttpsCreateSockTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); uc.setHostnameVerifier(new AllHostnameVerifier()); if (uc instanceof javax.net.ssl.HttpsURLConnection) { ((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory()); System.out.println("Using TestSocketFactory"); } uc.connect(); System.out.println("CONNECTED " + uc); System.out.println(uc.getResponseMessage()); uc.disconnect(); }
Example 9
Source File: HttpsSocketFacTest.java From hottub with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); uc.setSSLSocketFactory(sssf); uc.setHostnameVerifier(new AllHostnameVerifier()); InputStream is = uc.getInputStream(); byte[] ba = new byte[1024]; int read = 0; while ((read = is.read(ba)) != -1) { System.out.println(new String(ba, 0, read)); } System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); if (!sssf.socketCreated) throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); }
Example 10
Source File: SelfSignSslHurlStack.java From base-module with Apache License 2.0 | 6 votes |
@Override protected HttpURLConnection createConnection(URL url) throws IOException { //客户端有导入签名的用这种 // if ("https".equals(url.getProtocol()) && socketFactoryMap != null && socketFactoryMap.containsKey(url.getHost())) { // HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(okHttpClient).open(url); // connection.setSSLSocketFactory(socketFactoryMap.get(url.getHost())); // return connection; // } //无条件支持签名的用这种 if ("https".equals(url.getProtocol())) { HttpsURLConnection connection = ((HttpsURLConnection) super.createConnection(url)); try { connection.setSSLSocketFactory(sslSocketFactory); connection.setHostnameVerifier(myHostnameVerifier); } catch (Exception e) { e.printStackTrace(); } return connection; } else { return super.createConnection(url); } }
Example 11
Source File: HttpsSocketFacTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
void doClient() throws IOException { InetSocketAddress address = httpsServer.getAddress(); URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/"); System.out.println("trying to connect to " + url + "..."); HttpsURLConnection uc = (HttpsURLConnection) url.openConnection(); SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory(); uc.setSSLSocketFactory(sssf); uc.setHostnameVerifier(new AllHostnameVerifier()); InputStream is = uc.getInputStream(); byte[] ba = new byte[1024]; int read = 0; while ((read = is.read(ba)) != -1) { System.out.println(new String(ba, 0, read)); } System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated); System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped); if (!sssf.socketCreated) throw new RuntimeException("Failed: Socket Factory not being called to create Socket"); }
Example 12
Source File: Webb.java From gama with GNU General Public License v3.0 | 5 votes |
private void prepareSslConnection(final HttpURLConnection connection) { if ((hostnameVerifier != null || sslSocketFactory != null) && connection instanceof HttpsURLConnection) { final HttpsURLConnection sslConnection = (HttpsURLConnection) connection; if (hostnameVerifier != null) { sslConnection.setHostnameVerifier(hostnameVerifier); } if (sslSocketFactory != null) { sslConnection.setSSLSocketFactory(sslSocketFactory); } } }
Example 13
Source File: HttpKit.java From mblog with GNU General Public License v3.0 | 5 votes |
private static HttpsURLConnection initHttps(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException { TrustManager[] tm = {new MyX509TrustManager()}; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new SecureRandom()); SSLSocketFactory ssf = sslContext.getSocketFactory(); URL _url = new URL(url); HttpsURLConnection http = (HttpsURLConnection) _url.openConnection(); http.setHostnameVerifier(new TrustAnyHostnameVerifier()); http.setConnectTimeout(25000); http.setReadTimeout(25000); http.setRequestMethod(method); http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); http.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"); if ((headers != null) && (!headers.isEmpty())) { for (Entry entry : headers.entrySet()) { http.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); } } http.setSSLSocketFactory(ssf); http.setDoOutput(true); http.setDoInput(true); http.connect(); return http; }
Example 14
Source File: OneWaySSLBase.java From timely with Apache License 2.0 | 5 votes |
@Override protected HttpsURLConnection getUrlConnection(URL url) throws Exception { HttpsURLConnection.setDefaultSSLSocketFactory(getSSLSocketFactory()); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setHostnameVerifier((host, session) -> true); return con; }
Example 15
Source File: NetworkTools.java From MyBox with Apache License 2.0 | 5 votes |
public static boolean download(String address, File targetFile) { try { if (targetFile == null || address == null) { return false; } File tmpFile = FileTools.getTempFile(); URL url = new URL(address); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); SSLContext sc = SSLContext.getInstance(CommonValues.HttpsProtocal); sc.init(null, trustAllManager(), new SecureRandom()); conn.setSSLSocketFactory(sc.getSocketFactory()); conn.setHostnameVerifier(trustAllVerifier()); InputStream inStream = conn.getInputStream(); FileOutputStream fs = new FileOutputStream(tmpFile); byte[] buf = new byte[1204]; int len; while ((len = inStream.read(buf)) != -1) { fs.write(buf, 0, len); } if (targetFile.exists()) { targetFile.delete(); } tmpFile.renameTo(targetFile); return targetFile.exists(); } catch (Exception e) { logger.error(e.toString()); return false; } }
Example 16
Source File: Fetcher.java From hadoop with Apache License 2.0 | 5 votes |
@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; }
Example 17
Source File: HdfsProxy.java From RDFS with Apache License 2.0 | 5 votes |
private static HttpsURLConnection openConnection(String hostname, int port, String path) throws IOException { try { final URL url = new URI("https", null, hostname, port, path, null, null) .toURL(); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); // bypass hostname verification conn.setHostnameVerifier(new DummyHostnameVerifier()); conn.setRequestMethod("GET"); return conn; } catch (URISyntaxException e) { throw (IOException) new IOException().initCause(e); } }
Example 18
Source File: SecureClientUtils.java From atlas with Apache License 2.0 | 5 votes |
public URLConnectionClientHandler getUrlConnectionClientHandler() { return new URLConnectionClientHandler(new HttpURLConnectionFactory() { @Override public HttpURLConnection getHttpURLConnection(URL url) throws IOException { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (connection instanceof HttpsURLConnection) { LOG.debug("Attempting to configure HTTPS connection using client " + "configuration"); final SSLFactory factory; final SSLSocketFactory sf; final HostnameVerifier hv; try { Configuration conf = new Configuration(); conf.addResource(conf.get(SSLFactory.SSL_CLIENT_CONF_KEY, SecurityProperties.SSL_CLIENT_PROPERTIES)); UserGroupInformation.setConfiguration(conf); HttpsURLConnection c = (HttpsURLConnection) connection; factory = getSSLFactory(conf); sf = factory.createSSLSocketFactory(); hv = factory.getHostnameVerifier(); c.setSSLSocketFactory(sf); c.setHostnameVerifier(hv); } catch (Exception e) { LOG.info("Unable to configure HTTPS connection from " + "configuration. Leveraging JDK properties."); } } return connection; } }); }
Example 19
Source File: KMSClientProvider.java From big-c with Apache License 2.0 | 5 votes |
private HttpURLConnection configureConnection(HttpURLConnection conn) throws IOException { if (sslFactory != null) { HttpsURLConnection httpsConn = (HttpsURLConnection) conn; try { httpsConn.setSSLSocketFactory(sslFactory.createSSLSocketFactory()); } catch (GeneralSecurityException ex) { throw new IOException(ex); } httpsConn.setHostnameVerifier(sslFactory.getHostnameVerifier()); } return conn; }
Example 20
Source File: HttpTestHelper.java From qpid-broker-j with Apache License 2.0 | 4 votes |
public HttpURLConnection openManagementConnection(String path, String method) throws IOException { if (!path.startsWith("/")) { path = API_BASE + path; } final URL url = getManagementURL(path); LOGGER.debug("Opening connection : {} {}", method, url); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); if (httpCon instanceof HttpsURLConnection) { HttpsURLConnection httpsCon = (HttpsURLConnection) httpCon; try { SSLContext sslContext = SSLUtil.tryGetSSLContext(); TrustManager[] trustAllCerts = new TrustManager[] {new TrustAllTrustManager()}; KeyManager[] keyManagers = null; if (_keyStore != null) { char[] keyStoreCharPassword = _keyStorePassword == null ? null : _keyStorePassword.toCharArray(); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(_keyStore, keyStoreCharPassword); keyManagers = kmf.getKeyManagers(); } sslContext.init(keyManagers, trustAllCerts, null); httpsCon.setSSLSocketFactory(sslContext.getSocketFactory()); httpsCon.setHostnameVerifier((s, sslSession) -> true); } catch (KeyStoreException | UnrecoverableKeyException | KeyManagementException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } } httpCon.setConnectTimeout(_connectTimeout); if (_requestHostName != null) { httpCon.setRequestProperty("Host", _requestHostName); } if(_username != null) { String encoded = Base64.getEncoder().encodeToString((_username + ":" + _password).getBytes(UTF_8)); httpCon.setRequestProperty("Authorization", "Basic " + encoded); } if (_acceptEncoding != null && !"".equals(_acceptEncoding)) { httpCon.setRequestProperty("Accept-Encoding", _acceptEncoding); } httpCon.setDoOutput(true); httpCon.setRequestMethod(method); return httpCon; }