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

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

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

HttpsURLConnection.setSSLSocketFactory介绍

[英]Sets the SSL socket factory for this instance.
[中]设置此实例的SSL套接字工厂。

代码示例

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

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

代码示例来源:origin: igniterealtime/Smack

protected HttpURLConnection getHttpUrlConnectionFor(URL url) throws IOException {
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    if (sinttestConfiguration.tlsContext != null && urlConnection instanceof HttpsURLConnection) {
      HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
      httpsUrlConnection.setSSLSocketFactory(sinttestConfiguration.tlsContext.getSocketFactory());
    }
    return urlConnection;
  }
}

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

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
URL url = new URL("https://gridserver:3049/cgi-bin/ls.py");
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);
}

代码示例来源: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: Dreampie/Resty

url = new URL(apiUrl + httpClientRequest.getEncodedRestPath() + "?" + httpClientRequest.getEncodedJsonParam());
 } else {
  url = new URL(apiUrl + httpClientRequest.getEncodedRestPath());
url = new URL(apiUrl + httpClientRequest.getEncodedUrl());
conn = openHttpURLConnection(url, httpClientRequest, httpMethod);
((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory);
((HttpsURLConnection) conn).setHostnameVerifier(trustAnyHostnameVerifier);

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

SSLSocketFactory sf = ... see that example
URL url = new URL("https://google.com");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(sf);
... Now do stuff with that connection like GET or POST

代码示例来源:origin: nutzam/nutz

conn = (HttpURLConnection) request.getUrl().openConnection(proxy);
    conn.setConnectTimeout(connTime);
    conn.setInstanceFollowRedirects(followRedirects);
conn = (HttpURLConnection) url.openConnection();
if (conn instanceof HttpsURLConnection) {
  HttpsURLConnection httpsc = (HttpsURLConnection)conn;
  if (sslSocketFactory != null)
    httpsc.setSSLSocketFactory(sslSocketFactory);
  else if (Http.sslSocketFactory != null)
    httpsc.setSSLSocketFactory(Http.sslSocketFactory);
  if (hostnameVerifier != null)
    httpsc.setHostnameVerifier(hostnameVerifier);
  else if (Http.hostnameVerifier != null)
    httpsc.setHostnameVerifier(Http.hostnameVerifier);

代码示例来源:origin: igniterealtime/Smack

final HttpURLConnection urlConnection = (HttpURLConnection) putUrl.openConnection();
if (tlsSocketFactory != null && urlConnection instanceof HttpsURLConnection) {
  HttpsURLConnection httpsUrlConnection = (HttpsURLConnection) urlConnection;
  httpsUrlConnection.setSSLSocketFactory(tlsSocketFactory);

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

HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
    try {
      httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory());
      httpsURLConnection.setHostnameVerifier(getHostnameVerifier());
    } catch (Exception e) {
      e.printStackTrace();
sslContext.init(null, wrappedTrustManagers, null);
return sslContext.getSocketFactory();

代码示例来源:origin: ACRA/acra

@SuppressWarnings("WeakerAccess")
protected void configureHttps(@NonNull HttpsURLConnection connection) throws GeneralSecurityException {
  // Configure SSL
  final String algorithm = TrustManagerFactory.getDefaultAlgorithm();
  final TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
  final KeyStore keyStore = KeyStoreHelper.getKeyStore(context, config);
  tmf.init(keyStore);
  final SSLContext sslContext = SSLContext.getInstance("TLS");
  sslContext.init(null, tmf.getTrustManagers(), null);
  connection.setSSLSocketFactory(sslContext.getSocketFactory());
}

代码示例来源: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: cymcsg/UltimateAndroid

/**
 * @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, "");
  }
}

代码示例来源:origin: yanzhenjie/NoHttp

@Override
public Network execute(BasicRequest<?> request) throws Exception {
  URL url = new URL(request.url());
  HttpURLConnection connection = URLConnectionFactory.getInstance().open(url, request.getProxy());
  connection.setConnectTimeout(request.getConnectTimeout());
    SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
    if (sslSocketFactory != null)
      ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
    HostnameVerifier hostnameVerifier = request.getHostnameVerifier();
    if (hostnameVerifier != null)
      ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);

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

(HttpsURLConnection)url.openConnection();
SSLSocketFactory sf = context.getSocketFactory();
sf = new DOSSLSocketFactory(sf, cArray);
urlConnection.setSSLSocketFactory(sf);
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine;

代码示例来源:origin: org.jsoup/jsoup

private static HttpURLConnection createConnection(Connection.Request req) throws IOException {
  final HttpURLConnection conn = (HttpURLConnection) (
    req.proxy() == null ?
    req.url().openConnection() :
    req.url().openConnection(req.proxy())
  );
      ((HttpsURLConnection) conn).setSSLSocketFactory(socketFactory);
    } else if (!req.validateTLSCertificates()) {
      initUnSecureTSL();
      ((HttpsURLConnection)conn).setSSLSocketFactory(sslSocketFactory);
      ((HttpsURLConnection)conn).setHostnameVerifier(getInsecureVerifier());

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

private static String readOut(URL url) throws Exception {
 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
 conn.setSSLSocketFactory(clientSslFactory.createSSLSocketFactory());
 InputStream in = conn.getInputStream();
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 IOUtils.copyBytes(in, out, 1024);
 return out.toString();
}

代码示例来源:origin: briandilley/jsonrpc4j

private void setupSsl(HttpURLConnection connection) {
  if (HttpsURLConnection.class.isInstance(connection)) {
    HttpsURLConnection https = HttpsURLConnection.class.cast(connection);
    if (hostNameVerifier != null) {
      https.setHostnameVerifier(hostNameVerifier);
    }
    if (sslContext != null) {
      https.setSSLSocketFactory(sslContext.getSocketFactory());
    }
  }
}

代码示例来源:origin: jmdhappy/xxpay-master

protected void httpsPostMethod(String url, byte[] postData,
                SSLContext sslContext) throws IOException {
  SSLSocketFactory sf = sslContext.getSocketFactory();
  HttpsURLConnection conn = HttpClientUtil.getHttpsURLConnection(url);
  conn.setSSLSocketFactory(sf);
  this.doPost(conn, postData);
}

代码示例来源: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;
  }
}

相关文章

HttpsURLConnection类方法