javax.net.ssl.HttpsURLConnection.setHostnameVerifier()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(493)

本文整理了Java中javax.net.ssl.HttpsURLConnection.setHostnameVerifier()方法的一些代码示例,展示了HttpsURLConnection.setHostnameVerifier()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpsURLConnection.setHostnameVerifier()方法的具体详情如下:
包路径:javax.net.ssl.HttpsURLConnection
类名称:HttpsURLConnection
方法名:setHostnameVerifier

HttpsURLConnection.setHostnameVerifier介绍

[英]Sets the hostname verifier for this instance.
[中]设置此实例的主机名验证程序。

代码示例

代码示例来源:origin: SonarSource/sonarqube

void trust(HttpURLConnection connection) {
 if (connection instanceof HttpsURLConnection) {
  HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
  httpsConnection.setSSLSocketFactory(socketFactory);
  httpsConnection.setHostnameVerifier(hostnameVerifier);
 }
}

代码示例来源:origin: jfinal/jfinal

private static HttpURLConnection getHttpConnection(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
  URL _url = new URL(url);
  HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
  if (conn instanceof HttpsURLConnection) {
    ((HttpsURLConnection)conn).setSSLSocketFactory(sslSocketFactory);
    ((HttpsURLConnection)conn).setHostnameVerifier(trustAnyHostnameVerifier);
  }
  
  conn.setRequestMethod(method);
  conn.setDoOutput(true);
  conn.setDoInput(true);
  
  conn.setConnectTimeout(19000);
  conn.setReadTimeout(19000);
  
  conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  conn.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<String, String> entry : headers.entrySet()) {
      conn.setRequestProperty(entry.getKey(), entry.getValue());
    }
  }
  
  return conn;
}

代码示例来源:origin: stackoverflow.com

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new HostnameVerifier() {
  @Override
  public boolean verify(String arg0, SSLSession arg1) {

代码示例来源:origin: Javen205/IJPay

private static HttpURLConnection getHttpConnection(String url, String method, Map<String, String> headers) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
  URL _url = new URL(url);
  HttpURLConnection conn = (HttpURLConnection)_url.openConnection();
  if (conn instanceof HttpsURLConnection) {
    ((HttpsURLConnection)conn).setSSLSocketFactory(sslSocketFactory);
    ((HttpsURLConnection)conn).setHostnameVerifier(trustAnyHostnameVerifier);
  }
  
  conn.setRequestMethod(method);
  conn.setDoOutput(true);
  conn.setDoInput(true);
  
  conn.setConnectTimeout(19000);
  conn.setReadTimeout(19000);
  
  conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  conn.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<String, String> entry : headers.entrySet())
      conn.setRequestProperty(entry.getKey(), entry.getValue());
  
  return conn;
}

代码示例来源:origin: stackoverflow.com

public ServiceConnectionSE(String url) throws IOException {
  try {
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
  } catch (Exception e) {
    e.getMessage();
  }
  connection = (HttpsURLConnection) new URL(url).openConnection();
  ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

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;
}

代码示例来源:origin: wildfly/wildfly

private HttpURLConnection openConnection(URL url, SSLContext sslContext, HostnameVerifier hostnameVerifier) throws IOException {
  Assert.checkNotNullParam("url", url);
  boolean isHttps = url.getProtocol().equalsIgnoreCase("https");
  try {
    log.debugf("Opening connection to token introspection endpoint [%s]", url);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    if (isHttps) {
      HttpsURLConnection https = (HttpsURLConnection) connection;
      https.setSSLSocketFactory(sslContext.getSocketFactory());
      if (hostnameVerifier != null) {
        https.setHostnameVerifier(hostnameVerifier);
      }
    }
    return connection;
  } catch (IOException cause) {
    throw cause;
  }
}

代码示例来源:origin: stackoverflow.com

public ServiceConnectionSE(Proxy proxy, String url) throws IOException {
  try {
    SSLContext sc = SSLContext.getInstance("TLS");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
   } catch (Exception e) {
    e.getMessage();
   }
   connection = (HttpsURLConnection) new URL(url).openConnection();
  ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier());

  connection.setUseCaches(false);
  connection.setDoOutput(true);
  connection.setDoInput(true);
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

/**
  * If the given {@link HttpURLConnection} is an {@link HttpsURLConnection}
  * configures the connection with the {@link SSLSocketFactory} and
  * {@link HostnameVerifier} of this SSLFactory, otherwise does nothing.
  *
  * @param conn the {@link HttpURLConnection} instance to configure.
  * @return the configured {@link HttpURLConnection} instance.
  *
  * @throws IOException if an IO error occurred.
  */
 @Override
 public HttpURLConnection configure(HttpURLConnection conn)
  throws IOException {
  if (conn instanceof HttpsURLConnection) {
   HttpsURLConnection sslConn = (HttpsURLConnection) conn;
   try {
    sslConn.setSSLSocketFactory(createSSLSocketFactory());
   } catch (GeneralSecurityException ex) {
    throw new IOException(ex);
   }
   sslConn.setHostnameVerifier(getHostnameVerifier());
   conn = sslConn;
  }
  return conn;
 }
}

代码示例来源:origin: wildfly/wildfly

private HttpURLConnection openConnection() throws IOException {
  saslOAuth2.debugf("Opening connection to [%s]", tokenEndpointUri);
  HttpURLConnection connection = (HttpURLConnection) tokenEndpointUri.openConnection();
  SSLContext sslContext = resolveSSLContext();
  if (sslContext != null) {
    HttpsURLConnection https = (HttpsURLConnection) connection;
    https.setSSLSocketFactory(sslContext.getSocketFactory());
    if (hostnameVerifierSupplier != null) {
      https.setHostnameVerifier(checkNotNullParam("hostnameVerifier", hostnameVerifierSupplier.get()));
    }
  }
  return connection;
}

代码示例来源:origin: com.liferay.portal/com.liferay.portal.kernel

(HttpURLConnection)url.openConnection();
  (HttpsURLConnection)httpURLConnection;
httpsURLConnection.setHostnameVerifier(
  new HostnameVerifier() {

代码示例来源:origin: jersey/jersey

/**
 * Secure connection if necessary.
 * <p/>
 * Provided implementation sets {@link HostnameVerifier} and {@link SSLSocketFactory} to give connection, if that
 * is an instance of {@link HttpsURLConnection}.
 *
 * @param client client associated with this client runtime.
 * @param uc     http connection to be secured.
 */
protected void secureConnection(final JerseyClient client, final HttpURLConnection uc) {
  if (uc instanceof HttpsURLConnection) {
    HttpsURLConnection suc = (HttpsURLConnection) uc;
    final HostnameVerifier verifier = client.getHostnameVerifier();
    if (verifier != null) {
      suc.setHostnameVerifier(verifier);
    }
    if (HttpsURLConnection.getDefaultSSLSocketFactory() == suc.getSSLSocketFactory()) {
      // indicates that the custom socket factory was not set
      suc.setSSLSocketFactory(sslSocketFactory.get());
    }
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private static HttpURLConnection getConnection(URL url, String method, String ctype, Map<String, String> headerMap) throws IOException {
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  if (conn instanceof HttpsURLConnection) {
    HttpsURLConnection connHttps = (HttpsURLConnection) conn;
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[]{new TrustAllTrustManager()}, new SecureRandom());
        connHttps.setSSLSocketFactory(ctx.getSocketFactory());
        connHttps.setHostnameVerifier(new HostnameVerifier() {
          public boolean verify(String hostname, SSLSession session) {
            return true;

代码示例来源:origin: stackoverflow.com

URLConnection conn;

// ...

conn = new URL(...).openConnection();

// ...

if (conn instanceof HttpsURLConnection) {
  HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
  httpsConn.setHostnameVerifier(...);
}

// ...

代码示例来源:origin: jersey/jersey

/**
 * Secure connection if necessary.
 * <p/>
 * Provided implementation sets {@link HostnameVerifier} and {@link SSLSocketFactory} to give connection, if that
 * is an instance of {@link HttpsURLConnection}.
 *
 * @param client client associated with this client runtime.
 * @param uc     http connection to be secured.
 */
protected void secureConnection(final JerseyClient client, final HttpURLConnection uc) {
  if (uc instanceof HttpsURLConnection) {
    HttpsURLConnection suc = (HttpsURLConnection) uc;
    final HostnameVerifier verifier = client.getHostnameVerifier();
    if (verifier != null) {
      suc.setHostnameVerifier(verifier);
    }
    if (HttpsURLConnection.getDefaultSSLSocketFactory() == suc.getSSLSocketFactory()) {
      // indicates that the custom socket factory was not set
      suc.setSSLSocketFactory(sslSocketFactory.get());
    }
  }
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private static HttpURLConnection getConnection(URL url, String method, String ctype, Map<String, String> headerMap) throws IOException {
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  if (conn instanceof HttpsURLConnection) {
    HttpsURLConnection connHttps = (HttpsURLConnection) conn;
        SSLContext ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[]{new TrustAllTrustManager()}, new SecureRandom());
        connHttps.setSSLSocketFactory(ctx.getSocketFactory());
        connHttps.setHostnameVerifier(new HostnameVerifier() {
          public boolean verify(String hostname, SSLSession session) {
            return true;

代码示例来源:origin: stackoverflow.com

System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
InputStream stream_content=null;
try
  {URL url=new URL("https://74.125.28.103/");
  HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
  conn.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER );
  conn.setDoOutput(true);
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Host", "www.google.com");
  stream_content=conn.getInputStream();
  }
catch (Exception e) {}

代码示例来源:origin: looly/hutool

httpsConn.setHostnameVerifier(null != hostnameVerifier ? hostnameVerifier : new TrustAnyHostnameVerifier());
if (null == ssf) {
  if (StrUtil.equalsIgnoreCase("dalvik", System.getProperty("java.vm.name"))) {
httpsConn.setSSLSocketFactory(ssf);

代码示例来源:origin: apache/ignite

URLConnection conn = url.openConnection();
  https.setHostnameVerifier(new DeploymentHostnameVerifier());
  https.setSSLSocketFactory(ctx.getSocketFactory());

代码示例来源:origin: net.aequologica.neo/geppaequo-core

private void callHTTPSServer(HttpServletResponse response, URL url) throws IOException {
  trustAllHosts();
  HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
  https.setHostnameVerifier(DO_NOT_VERIFY);
  try (BufferedReader br = new BufferedReader(new InputStreamReader(https.getInputStream()))) {
    String inputLine;
    while ((inputLine = br.readLine()) != null) {
      response.getWriter().println(inputLine);
    }
  }
}

相关文章

HttpsURLConnection类方法