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

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

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

HttpsURLConnection.getResponseCode介绍

暂无

代码示例

代码示例来源:origin: jberkel/sms-backup-plus

public OAuth2Token refreshToken(String refreshToken) throws IOException {
  HttpsURLConnection connection = postTokenEndpoint(getRefreshTokenPostData(refreshToken));
  final int responseCode = connection.getResponseCode();
  if (responseCode == HttpsURLConnection.HTTP_OK) {
    return parseResponse(connection.getInputStream());
  } else {
    Log.e(TAG, "error: " + responseCode);
    throw new IOException("Invalid response from server:" + responseCode);
  }
}

代码示例来源:origin: jberkel/sms-backup-plus

private String getUsernameFromContacts(OAuth2Token token) {
  try {
    HttpsURLConnection connection = (HttpsURLConnection) new URL(CONTACTS_URL).openConnection();
    connection.addRequestProperty("Authorization", "Bearer "+token.accessToken);
    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
      final InputStream inputStream = connection.getInputStream();
      String email = extractEmail(inputStream);
      inputStream.close();
      return email;
    } else {
      Log.w(TAG, String.format("unexpected server response: %d (%s)",
          connection.getResponseCode(), connection.getResponseMessage()));
      return null;
    }
  } catch (SAXException e) {
    Log.e(TAG, ERROR, e);
    return null;
  } catch (IOException e) {
    Log.e(TAG, ERROR, e);
    return null;
  } catch (ParserConfigurationException e) {
    Log.e(TAG, ERROR, e);
    return null;
  }
}

代码示例来源:origin: jberkel/sms-backup-plus

public OAuth2Token getToken(String code) throws IOException {
  HttpsURLConnection connection = postTokenEndpoint(getAccessTokenPostData(code));
  final int responseCode = connection.getResponseCode();
  if (responseCode == HttpsURLConnection.HTTP_OK) {
    OAuth2Token token = parseResponse(connection.getInputStream());
    String username = getUsernameFromContacts(token);
    Log.d(TAG, "got token " + token.getTokenForLogging()+ ", username="+username);
    return new OAuth2Token(token.accessToken, token.tokenType, token.refreshToken, token.expiresIn, username);
  } else {
    Log.e(TAG, "error: " + responseCode);
    throw new IOException("Invalid response from server:" + responseCode);
  }
}

代码示例来源:origin: Azure/azure-iot-sdk-java

/**
 * Returns the response status code.
 *
 * @return The response status code.
 *
 * @throws IOException This exception thrown if no response was received.
 */
public int getResponseStatus() throws IOException
{
  // Codes_SRS_SERVICE_SDK_JAVA_HTTPCONNECTION_12_020: [The function shall return the response status code.]
  // Codes_SRS_SERVICE_SDK_JAVA_HTTPCONNECTION_12_021: [The function shall throw an IOException if no response was received.]
  return this.connection.getResponseCode();
}

代码示例来源:origin: com.microsoft.azure.iothub-java-client/iothub-java-device-client

/**
 * Returns the response status code.
 *
 * @return the response status code.
 *
 * @throws IOException if no response was received.
 */
public int getResponseStatus() throws IOException
{
  // Codes_SRS_HTTPSCONNECTION_11_015: [The function shall return the response status code.]
  // Codes_SRS_HTTPSCONNECTION_11_016: [The function shall throw an IOException if no response was received.]
  return this.connection.getResponseCode();
}

代码示例来源:origin: Azure/azure-iot-sdk-java

/**
 * Returns the response status code.
 *
 * @return The response status code.
 *
 * @throws IOException This exception thrown if no response was received.
 */
public int getResponseStatus() throws IOException
{
  // Codes_SRS_HTTPCONNECTION_25_020: [The function shall return the response status code.]
  // Codes_SRS_HTTPCONNECTION_25_021: [The function shall throw an IOException if no response was received.]
  return this.connection.getResponseCode();
}

代码示例来源:origin: ansitech/weixin4j

public Response(HttpsURLConnection https) throws IOException {
  this.https = https;
  this.status = https.getResponseCode();
  if (null == (is = https.getErrorStream())) {
    is = https.getInputStream();
  }
}

代码示例来源:origin: org.talend.components/components-marketo-runtime

protected InputStreamReader getReaderFromHttpResponse(HttpsURLConnection conn)
    throws MarketoException, IOException {
  int responseCode = conn.getResponseCode();
  if (responseCode == 200) {
    InputStream inStream = conn.getInputStream();
    return new InputStreamReader(inStream);
  } else {
    LOG.error("{} request failed: {}.", conn.getRequestMethod(), responseCode);
    throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
  }
}

代码示例来源:origin: Talend/components

protected InputStreamReader getReaderFromHttpResponse(HttpsURLConnection conn)
    throws MarketoException, IOException {
  int responseCode = conn.getResponseCode();
  if (responseCode == 200) {
    InputStream inStream = conn.getInputStream();
    return new InputStreamReader(inStream);
  } else {
    LOG.error("{} request failed: {}.", conn.getRequestMethod(), responseCode);
    throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
  }
}

代码示例来源:origin: net.roboconf/roboconf-iaas-azure

private int processDeleteRequest(URL url, String keyStore, String keyStorePassword)
throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
  SSLSocketFactory sslFactory = this.getSSLSocketFactory(keyStore, keyStorePassword);
  HttpsURLConnection con = null;
  con = (HttpsURLConnection) url.openConnection();
  con.setSSLSocketFactory(sslFactory);
  con.setRequestMethod("DELETE");
  con.addRequestProperty("x-ms-version", "2014-04-01");
  return con.getResponseCode();
}

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

private String call(String method, HttpsURLConnection conn) throws IOException {
    LOG.info("  Calling {} {}. Turn on debug logging to see response headers.", method, conn.getURL());

    if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
      return IOUtil.readFully(conn.getInputStream());
    } else {
      String errorMessage =
        String.format("%s %s failed, response code = %s, response body:\n%s",
          method, conn.getURL(), conn.getResponseCode(), IOUtil.readFully(conn.getErrorStream()));
      throw new IOException(errorMessage);
    }
  }
}

代码示例来源:origin: mockito/shipkit

private String call(String method, HttpsURLConnection conn) throws IOException {
    LOG.info("  Calling {} {}. Turn on debug logging to see response headers.", method, conn.getURL());

    if (conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
      return IOUtil.readFully(conn.getInputStream());
    } else {
      String errorMessage =
        String.format("%s %s failed, response code = %s, response body:\n%s",
          method, conn.getURL(), conn.getResponseCode(), IOUtil.readFully(conn.getErrorStream()));
      throw new IOException(errorMessage);
    }
  }
}

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

private void setResponseData(@NonNull byte[] byteArray) throws IOException {
  InputStream inputStream = new ByteArrayInputStream(byteArray);
  doReturn(inputStream).when(httpsURLConnection).getInputStream();
  doReturn(HttpURLConnection.HTTP_OK).when(httpsURLConnection).getResponseCode();
}

代码示例来源: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: line/line-sdk-android

private void setErrorData(int httpErrorCode, @NonNull byte[] byteArray) throws IOException {
  InputStream inputStream = new ByteArrayInputStream(byteArray);
  doReturn(inputStream).when(httpsURLConnection).getErrorStream();
  doReturn(httpErrorCode).when(httpsURLConnection).getResponseCode();
}

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

@Test
public void testPing() throws Exception {
 final URL url = new URL("https://"+ SERVER_HOST + ":" + webServerPort + "/ping");
 Properties systemProps = System.getProperties();
 systemProps.put( "javax.net.ssl.trustStore", Resources.getResource("cacerts.jks").getPath());
 System.setProperties(systemProps);
 HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
 Assert.assertEquals(HttpsURLConnection.HTTP_OK, conn.getResponseCode());
 String response = IOUtils.toString(conn.getInputStream());
 Assert.assertEquals("pong\n", response);
}

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

protected void doHttpsUpload(InputStream in, long contentLength, URI uri) throws IOException {
  HttpsURLConnection conn = getHttpsConnection(contentLength, uri);
  IOUtils.copy(in, conn.getOutputStream());
  int responseCode = conn.getResponseCode();
  assertTrue(conn.getResponseMessage(), responseCode < 400);
}

代码示例来源:origin: apache/jackrabbit-oak

protected void doHttpsUpload(InputStream in, long contentLength, URI uri) throws IOException {
    HttpsURLConnection conn = getHttpsConnection(contentLength, uri);
    IOUtils.copy(in, conn.getOutputStream());
    int responseCode = conn.getResponseCode();
    assertTrue(conn.getResponseMessage(), responseCode < 400);
  }
}

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

相关文章

HttpsURLConnection类方法