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

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

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

HttpsURLConnection.getSSLSocketFactory介绍

[英]Returns the SSL socket factory used by this instance.
[中]返回此实例使用的SSL套接字工厂。

代码示例

代码示例来源: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: 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: org.glassfish.jersey.core/jersey-client

/**
 * 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: SonarSource/sonarqube

@Test
public void trustAllCerts() throws Exception {
 HttpsURLConnection connection1 = newHttpsConnection();
 HttpsTrust.INSTANCE.trust(connection1);
 assertThat(connection1.getSSLSocketFactory()).isNotNull();
 assertThat(connection1.getSSLSocketFactory().getDefaultCipherSuites()).isNotEmpty();
}

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

@Test
public void singleSslFactory() throws Exception {
 HttpsURLConnection connection1 = newHttpsConnection();
 HttpsTrust.INSTANCE.trust(connection1);
 HttpsURLConnection connection2 = newHttpsConnection();
 HttpsTrust.INSTANCE.trust(connection2);
 assertThat(connection1.getSSLSocketFactory()).isSameAs(connection2.getSSLSocketFactory());
}

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

javax.net.ssl.HttpsURLConnection.getSSLSocketFactory()

代码示例来源:origin: BiglySoftware/BiglyBT

public static void
DHHackIt(
  HttpsURLConnection    ssl_con )
{
  final SSLSocketFactory factory = ssl_con.getSSLSocketFactory();
  SSLSocketFactory hack = DHHackIt( factory );
  ssl_con.setSSLSocketFactory( hack );
}

代码示例来源:origin: moz1q1/WalleLibrary

/**
   * 信任所有
   *
   * @param connection
   * @return
   */
  public static SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
    SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
    try {
      SSLContext sc = SSLContext.getInstance("TLS");
      sc.init(null, trustAllCerts, new java.security.SecureRandom());
      SSLSocketFactory newFactory = sc.getSocketFactory();
      connection.setSSLSocketFactory(newFactory);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return oldFactory;
  }
}

代码示例来源:origin: com.github.mcpat.gcf/gcf-standard

public HttpsConnectionImpl(HttpsURLConnection connection) {
  super(connection);
  
  connection.setSSLSocketFactory(new InterceptingSSLSocketFactory(connection.getSSLSocketFactory()));
}

代码示例来源:origin: line/line-sdk-android

@VisibleForTesting
@NonNull
protected HttpURLConnection openHttpConnection(@NonNull Uri uri) throws IOException {
  URLConnection urlConnection = new URL(uri.toString()).openConnection();
  if (!(urlConnection instanceof HttpsURLConnection)) {
    if (BuildConfig.DEBUG) {
      return (HttpURLConnection) urlConnection;
    }
    throw new IllegalArgumentException("The scheme of the server url must be https." + uri);
  }
  // Android 6.0 doesn't enable SSLv3 by default, but it does enable some weak ciphers like
  // TLS_ECDHE_RSA_WITH_RC4_128_SHA. The custom socket factory (TLSSocketFactory) also disables
  // weak ciphers, so apply this. Android 7.0 uses safe defaults so doesn't apply this.
  if (Build.VERSION.SDK_INT >= 24 /* Build.VERSION_CODES.N */) {
    return (HttpURLConnection) urlConnection;
  }
  HttpsURLConnection httpsURLConnection = (HttpsURLConnection) urlConnection;
  httpsURLConnection.setSSLSocketFactory(
      new TLSSocketFactory(httpsURLConnection.getSSLSocketFactory()));
  return httpsURLConnection;
}

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

private HttpsURLConnection openConnection(URL src, URL dest, SSLContext sslContext)
    throws IOException, ProtocolException {
  HttpsURLConnection connection = (HttpsURLConnection) dest.openConnection();
  HttpsHostNameVerifier httpsHostNameVerifier = new HttpsHostNameVerifier();
  connection.setHostnameVerifier(httpsHostNameVerifier);
  connection.setConnectTimeout(CONNECT_TIMEOUT);
  connection.setReadTimeout(READ_TIMEOUT);
  connection.setRequestMethod(POST_METHOD);
  connection.setRequestProperty(CONTENT_TYPE, SoapConstants.CONTENT_TYPE_HEADER);
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setSSLSocketFactory(sslContext.getSocketFactory());
  if ( src!=null ) {
    InetAddress inetAddress = InetAddress.getByName(src.getHost());
    int destPort = dest.getPort();
    if ( destPort <=0 ) 
      destPort=SERVER_HTTPS_PORT;
    int srcPort = src.getPort();
    if ( srcPort <=0 ) 
      srcPort=CLIENT_HTTPS_PORT;
     connectionSocket = connection.getSSLSocketFactory().createSocket(dest.getHost(), destPort, inetAddress, srcPort);
  }
  connection.connect();
  return connection;
}

代码示例来源:origin: com.eclipsesource.jaxrs/jersey-all

/**
 * 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: hstaudacher/osgi-jax-rs-connector

/**
 * 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: org.glassfish.jersey.bundles/jaxrs-ri

/**
 * 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: hstaudacher/osgi-jax-rs-connector

/**
 * 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: KostyaSha/yet-another-docker-plugin

/**
 * 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: guardianproject/NetCipher

@Test
  public void testDefaultSSLSocketFactory() throws IOException {
    SSLSocketFactory sslSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    assertFalse(sslSocketFactory instanceof TlsOnlySocketFactory);
    URL url = new URL("https://guardianproject.info");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    assertFalse(connection.getSSLSocketFactory() instanceof TlsOnlySocketFactory);
    connection.disconnect();

    HttpsURLConnection.setDefaultSSLSocketFactory(NetCipher.getTlsOnlySocketFactory());
    assertTrue(HttpsURLConnection.getDefaultSSLSocketFactory() instanceof TlsOnlySocketFactory);
    connection = (HttpsURLConnection) url.openConnection();
    assertTrue(connection.getSSLSocketFactory() instanceof TlsOnlySocketFactory);
    connection.disconnect();
  }
}

代码示例来源:origin: braintree/braintree_android

@Test(timeout = 1000)
public void usesSSLSocketFactory() throws IOException {
  SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
  HttpClient httpClient = new HttpClient()
      .setSSLSocketFactory(sslSocketFactory);
  HttpURLConnection connection = httpClient.init("https://example.com/");
  assertEquals(sslSocketFactory, ((HttpsURLConnection) connection).getSSLSocketFactory());
}

代码示例来源:origin: kiegroup/droolsjbpm-integration

/**
 * Verify single SSL socket factory is created across all calls
 */
@Test
public void singleSslSocketFactory() {
  KieServerHttpRequest request1 = getRequest("https://localhost").trustAllCerts();
  KieServerHttpRequest request2 = getRequest("https://localhost").trustAllCerts();
  assertNotNull(((HttpsURLConnection) request1.getConnection()).getSSLSocketFactory());
  assertNotNull(((HttpsURLConnection) request2.getConnection()).getSSLSocketFactory());
  assertEquals(((HttpsURLConnection) request1.getConnection()).getSSLSocketFactory(),
      ((HttpsURLConnection) request2.getConnection()).getSSLSocketFactory());
}

代码示例来源:origin: guardianproject/NetCipher

connection.setConnectTimeout(0); // blocking connect with TCP timeout
connection.setReadTimeout(20000);
SSLSocketFactory sslSocketFactory = connection.getSSLSocketFactory();
assertTrue(sslSocketFactory instanceof TlsOnlySocketFactory);
connection.getContent();

相关文章

HttpsURLConnection类方法