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

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

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

HttpsURLConnection.disconnect介绍

暂无

代码示例

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

inputStream.close();
inputStream = null;
conn.disconnect();
_log.info("响应数据,rtn={}", buffer);
jsonObject = JSONObject.parseObject(buffer.toString());

代码示例来源:origin: wuyouzhuguli/FEBS-Shiro

conn.disconnect();
  indata.close();
} catch (Exception e) {

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

IOUtils.closeQuietly(inputStream);
if (conn != null) {
  conn.disconnect();

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

IOUtils.closeQuietly(inputStream);
if (conn != null) {
  conn.disconnect();

代码示例来源:origin: org.demoiselle.signer/timestamp

@Override
  public void close() {    	
    try {
      this.HttpsConnector.disconnect();
      this.out.close();
    } catch (IOException e) {
      e.printStackTrace();
    } 
  }
}

代码示例来源:origin: DaylightingSociety/WhereAreTheEyes

@Override
  public void run() {
    String result = "";
    try {
      // Wait a second in case we just uploaded a pin
      // This allows the server time to process the pin, so it will appear in this request
      Thread.sleep(Constants.PIN_MARK_DELAY);
      URL url = new URL("https://" + Constants.DOMAIN + "/getScore/" + username);
      HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
      InputStream in = new BufferedInputStream(conn.getInputStream());
      java.util.Scanner s = new java.util.Scanner(in).useDelimiter("\\A");
      result = s.hasNext() ? s.next() : "";
      conn.disconnect();
      parseDownload(result);
    } catch (Exception e) {
      Log.d("Score", "Crash while downloading score: " + e.getMessage());
      StringWriter w = new StringWriter();
      PrintWriter pw = new PrintWriter(w);
      e.printStackTrace(pw);
      pw.flush();
      Log.d("Score", "Trace: " + w.toString());
    }
  }
});

代码示例来源:origin: gradle.plugin.com.rosberry.android.gradle/rawf

private String sendGet(String urlAddress) throws Exception {
  HttpsURLConnection connection = null;
  try {
    System.out.println("Request url: " + urlAddress);
    URL url = new URL(urlAddress);
    connection = (HttpsURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    initConnection(connection);
    addHeaders(connection);
    connection.connect();
    System.out.println("Response code: " + connection.getResponseCode());
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    return br.readLine();
  } finally {
    if (connection != null) {
      connection.disconnect();
    }
  }
}

代码示例来源:origin: ge0rg/MemorizingTrustManager

public void run() {
    try {
      URL u = new URL(urlString);
      HttpsURLConnection c = (HttpsURLConnection)u.openConnection();
      c.connect();
      setText("" + c.getResponseCode() + " "
          + c.getResponseMessage(), false);
      c.disconnect();
    } catch (Exception e) {
      setText(e.toString(), false);
      e.printStackTrace();
    }
  }
}.start();

代码示例来源:origin: vmware/admiral

private static void handleCertForHttpsThroughHttpProxyWithAuth(URL url, Proxy proxy,
    String proxyUsername, String proxyPassword, long timeout,
    SSLSocketFactory socketFactory) throws IOException {
  HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(proxy);
  connection.setSSLSocketFactory(socketFactory);
  connection.setConnectTimeout((int) (timeout < 0 ? 0 : timeout));
  if (proxyUsername != null && proxyPassword != null) {
    byte[] token = (proxyUsername + ":" + proxyPassword)
        .getBytes(StandardCharsets.UTF_8);
    connection.setRequestProperty(REQUEST_HEADER_PROXY_AUTHORIZATION,
        "Basic " + Base64.encodeBase64StringUnChunked(token));
  }
  connection.connect();
  connection.disconnect();
}

代码示例来源:origin: hazelcast/hazelcast-jet-demos

private JsonArray pollForAircraft() throws IOException {
  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  StringBuilder response = new StringBuilder();
  try {
    con.setRequestMethod("GET");
    con.addRequestProperty("User-Agent", "Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 40.0.2214.91 Safari / 537.36");
    int responseCode = con.getResponseCode();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
      String inputLine;
      while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
      }
    }
    if (responseCode != 200) {
      logger.info("API returned error: " + responseCode + " " + response);
      return new JsonArray();
    }
  } finally {
    con.disconnect();
  }
  JsonValue value = Json.parse(response.toString());
  JsonObject object = value.asObject();
  return object.get("acList").asArray();
}

代码示例来源:origin: Microsoft/azure-tools-for-java

@NotNull
protected static HttpResponse getResponse(@NotNull final String method,
                     @Nullable final String postData,
                     @NotNull HttpsURLConnection sslConnection)
    throws IOException, AzureCmdException {
  try {
    sslConnection.setRequestMethod(method);
    sslConnection.setDoOutput(postData != null);
    if (postData != null) {
      DataOutputStream wr = new DataOutputStream(sslConnection.getOutputStream());
      try {
        wr.writeBytes(postData);
        wr.flush();
      } finally {
        wr.close();
      }
    }
    return getResponse(sslConnection);
  } finally {
    sslConnection.disconnect();
  }
}

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

@org.junit.Test
public void testSSLConnectionUsingJavaAPIs() throws Exception {
  URL service = new URL("https://localhost:" + PORT);
  HttpsURLConnection connection = (HttpsURLConnection) service.openConnection();
  connection.setHostnameVerifier(new DisableCNCheckVerifier());
  SSLContext sslContext = SSLContext.getInstance("TLS");
  KeyStore ts = KeyStore.getInstance("JKS");
  try (InputStream trustStore =
    ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", ClientAuthTest.class)) {
    ts.load(trustStore, "password".toCharArray());
  }
  TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  tmf.init(ts);
  KeyStore ks = KeyStore.getInstance("JKS");
  try (InputStream keyStore =
    ClassLoaderUtils.getResourceAsStream("keys/Morpit.jks", ClientAuthTest.class)) {
    ks.load(keyStore, "password".toCharArray());
  }
  KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
  kmf.init(ks, "password".toCharArray());
  sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new java.security.SecureRandom());
  connection.setSSLSocketFactory(sslContext.getSocketFactory());
  connection.connect();
  connection.disconnect();
}

代码示例来源:origin: SecUSo/privacy-friendly-netmonitor

public static void assertApiResponseCode(String apiUrl, int expected) {
    int responseCode = -1;

    try {
      URL url = new URL(apiUrl);
      HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
      responseCode = conn.getResponseCode();

      conn.disconnect();
    } catch (Exception ignored) {
    }

    Assert.assertFalse("Failure in assertApiResponseCode method", responseCode == -1);
    Assert.assertTrue("ResponseCode is not the expected one. (IS: " + responseCode + "; SHOULD BE: " + expected + ")", responseCode == expected);
  }
}

代码示例来源:origin: apache/nifi-minifi

protected ConfigSchema assertReturnCode(String query, SSLContext sslContext, int expectedReturnCode) throws Exception {
  HttpsURLConnection httpsURLConnection = openUrlConnection(C2_URL + query, sslContext);
  try {
    assertEquals(expectedReturnCode, httpsURLConnection.getResponseCode());
    if (expectedReturnCode == 200) {
      return SchemaLoader.loadConfigSchemaFromYaml(httpsURLConnection.getInputStream());
    }
  } finally {
    httpsURLConnection.disconnect();
  }
  return null;
}

代码示例来源:origin: Microsoft/AppCenter-SDK-Android

@Test
public void get100() throws Exception {
  /* Configure mock HTTP. */
  URL url = mock(URL.class);
  whenNew(URL.class).withAnyArguments().thenReturn(url);
  HttpsURLConnection urlConnection = mock(HttpsURLConnection.class);
  when(url.openConnection()).thenReturn(urlConnection);
  when(urlConnection.getResponseCode()).thenReturn(100);
  ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  when(urlConnection.getOutputStream()).thenReturn(buffer);
  when(urlConnection.getErrorStream()).thenReturn(new ByteArrayInputStream("Continue".getBytes()));
  /* Configure API client. */
  DefaultHttpClient httpClient = new DefaultHttpClient();
  /* Test calling code. */
  Map<String, String> headers = new HashMap<>();
  ServiceCallback serviceCallback = mock(ServiceCallback.class);
  mockCall();
  httpClient.callAsync("", METHOD_POST, headers, null, serviceCallback);
  verify(serviceCallback).onCallFailed(new HttpException(100, "Continue"));
  verifyNoMoreInteractions(serviceCallback);
  verify(urlConnection).disconnect();
}

代码示例来源:origin: Microsoft/AppCenter-SDK-Android

@Test
public void failedSerialization() throws Exception {
  /* Configure mock HTTP. */
  URL url = mock(URL.class);
  whenNew(URL.class).withAnyArguments().thenReturn(url);
  HttpsURLConnection urlConnection = mock(HttpsURLConnection.class);
  when(url.openConnection()).thenReturn(urlConnection);
  /* Configure API client. */
  HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class);
  JSONException exception = new JSONException("mock");
  when(callTemplate.buildRequestBody()).thenThrow(exception);
  DefaultHttpClient httpClient = new DefaultHttpClient();
  /* Test calling code. */
  ServiceCallback serviceCallback = mock(ServiceCallback.class);
  mockCall();
  httpClient.callAsync("", METHOD_POST, new HashMap<String, String>(), callTemplate, serviceCallback);
  verify(serviceCallback).onCallFailed(exception);
  verifyNoMoreInteractions(serviceCallback);
  verify(urlConnection).disconnect();
  verifyStatic();
  TrafficStats.setThreadStatsTag(anyInt());
  verifyStatic();
  TrafficStats.clearThreadStatsTag();
}

代码示例来源:origin: cdapio/cdap

/**
 * Test request to server using a client certificate that is not trusted by the server.
 *
 * @throws Exception
 */
@Test
public void testInvalidClientCertForStatusEndpoint() throws Exception {
 HttpsURLConnection urlConn = openConnection(getURL(Constants.EndPoints.STATUS), "invalid-client.jks");
 try {
  // Request is Authorized
  assertEquals(200, urlConn.getResponseCode());
 } finally {
  urlConn.disconnect();
 }
}

代码示例来源:origin: cdapio/cdap

/**
 * Test request to server using a client certificate that is not trusted by the server.
 *
 * @throws Exception
 */
@Override
@Test
public void testInvalidAuthentication() throws Exception {
 HttpsURLConnection urlConn = openConnection(getURL(GrantAccessToken.Paths.GET_TOKEN), "invalid-client.jks");
 try {
  // Request is Unauthorized
  assertEquals(403, urlConn.getResponseCode());
 } finally {
  urlConn.disconnect();
 }
}

代码示例来源:origin: cdapio/cdap

/**
 * Test request to server without providing a client certificate
 *
 * @throws Exception
 */
@Test
public void testMissingClientCertAuthentication() throws Exception {
 HttpsURLConnection urlConn = new HttpsEnabler()
  .setTrustAll(true)
  .enable((HttpsURLConnection) openConnection(getURL(GrantAccessToken.Paths.GET_TOKEN)));
 try {
  // Status request is authorized without any extra headers
  assertEquals(403, urlConn.getResponseCode());
 } finally {
  urlConn.disconnect();
 }
}

相关文章

HttpsURLConnection类方法