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

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

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

HttpsURLConnection.addRequestProperty介绍

暂无

代码示例

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

conn.addRequestProperty("Authorization", "Bearer " + serviceAccountToken(accountToken));

代码示例来源: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: com.solidfire/jsvcgen-client-java

@Override
  public void accept(String token) {
    connection.addRequestProperty("Authorization", token);
  }
});

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

private InputStream read(String url, TextChannel channel) {
    try {
      HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
      conn.addRequestProperty("User-Agent", "Mozilla/5.0 FlareBot");

      return conn.getInputStream();
    } catch (IOException e) {
      MessageUtils.sendErrorMessage("Failed to jumbo image!\nMessage: " + e.getMessage(), channel);
      FlareBot.LOGGER.error("Failed to send image for " + getCommand() + " command. Guild ID: "
          + channel.getGuild().getId() + ", URL: " + url, e);
      return null;
    }
  }
}

代码示例来源:origin: EngineHub/CraftBook

/**
 * Sends the data to the bStats server.
 *
 * @param data The data to send.
 * @throws Exception If the request failed.
 */
private static void sendData(JsonObject data) throws Exception {
  Validate.notNull(data, "Data cannot be null");
  HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
  // Compress the data to save bandwidth
  byte[] compressedData = compress(data.toString());
  // Add headers
  connection.setRequestMethod("POST");
  connection.addRequestProperty("Accept", "application/json");
  connection.addRequestProperty("Connection", "close");
  connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
  connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
  connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
  connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
  // Send data
  connection.setDoOutput(true);
  DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
  outputStream.write(compressedData);
  outputStream.flush();
  outputStream.close();
  connection.getInputStream().close(); // We don't care about the response - Just send our data :)
}

代码示例来源:origin: org.avaje.k8s/k8s-discovery

protected K8sServiceMembers loadAllMembers() {
  String path = "/api/v1/namespaces/" + namespace + "/endpoints/" + serviceName;
  try {
    if (log.isTraceEnabled()) {
      log.trace("loading member content from:{}", masterUrl + path);
    }
    URL url = new URL(masterUrl + path);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setHostnameVerifier(trustAllHosts);
    SSLContext ctx = SSLContext.getInstance("SSL");
    ctx.init(null, trustAll, new SecureRandom());
    conn.setSSLSocketFactory(ctx.getSocketFactory());
    conn.addRequestProperty("Authorization", "Bearer " + serviceAccountToken(accountToken));
    String jsonContent = readContent(conn);
    if (log.isTraceEnabled()) {
      log.trace("K8s endpoints json: " + jsonContent);
    }
    return new MemberParser(jsonContent).parseJson();
  } catch (Exception e) {
    throw new IllegalStateException("Error getting members", e);
  }
}

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

@NotNull
public HttpsURLConnection getSSLConnection(@NotNull String managementUrl,
                      @NotNull String path,
                      @NotNull ContentType contentType)
    throws AzureCmdException {
  try {
    URL myUrl = new URL(managementUrl + path);
    HttpsURLConnection conn = (HttpsURLConnection) myUrl.openConnection();
    conn.addRequestProperty(USER_AGENT_HEADER, getPlatformUserAgent());
    conn.addRequestProperty(TELEMETRY_HEADER, getPlatformUserAgent());
    conn.addRequestProperty(X_MS_VERSION_HEADER, AZURE_API_VERSION);
    conn.addRequestProperty(ACCEPT_HEADER, "");
    conn.setReadTimeout(DEFAULT_READ_TIMEOUT);
    if (contentType != null) {
      conn.addRequestProperty(CONTENT_TYPE_HEADER, contentType.toString());
    }
    return conn;
  } catch (IOException e) {
    throw new AzureCmdException(e.getMessage(), e);
  }
}

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

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request.
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format.
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

代码示例来源:origin: InventivetalentDev/AnimatedFrames

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

代码示例来源:origin: aadnk/ProtocolLib

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

代码示例来源:origin: io.github.bedwarsrel/BedwarsRel-Common

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON

代码示例来源:origin: EngineHub/CommandHelper

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

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

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON

代码示例来源:origin: Bkm016/TabooLib

connection.addRequestProperty("Accept", "application/json");
connection.addRequestProperty("Connection", "close");
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);

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

private int processPostRequest(URL url, byte[] data, String contentType, 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.setDoOutput(true);
  con.setRequestMethod("POST");
  con.addRequestProperty("x-ms-version", "2014-04-01");
  con.setRequestProperty("Content-Length", String.valueOf(data.length));
  con.setRequestProperty("Content-Type", contentType);
  DataOutputStream  requestStream = new DataOutputStream (con.getOutputStream());
  requestStream.write(data);
  requestStream.flush();
  requestStream.close();
  return con.getResponseCode();
}

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

private String processGetRequest(URL url, String keyStore, String keyStorePassword)
throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
  SSLSocketFactory sslFactory = this.getSSLSocketFactory(keyStore, keyStorePassword);
  HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  con.setSSLSocketFactory(sslFactory);
  con.setRequestMethod("GET");
  con.addRequestProperty("x-ms-version", "2014-04-01");
  InputStream responseStream = (InputStream) con.getContent();
  try {
    return getStringFromInputStream(responseStream);
  } finally {
    Utils.closeQuietly( responseStream );
  }
}

代码示例来源: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: com.solidfire/jsvcgen-client-java

/**
 * Constructs a HTTPS POST connection
 *
 * @param connection the https connection to a Element OS cluster
 */
protected void prepareConnection(final HttpsURLConnection connection) {
  try {
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
  } catch (ProtocolException pe) {
    // ...because this could happen...ever...
    throw new RuntimeException("Your HTTP connection does not support \"POST\"", pe);
  }
  connection.addRequestProperty("Accept", "application/json");
  connection.setConnectTimeout(this.connectionTimeout);
  connection.setReadTimeout(this.readTimeout);
  authenticationToken.ifPresent(new Consumer<String>() {
    @Override
    public void accept(String token) {
      connection.addRequestProperty("Authorization", token);
    }
  });
}

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

@Override
  @NotNull
  public HttpsURLConnection getSSLConnection(@NotNull String managementUrl,
                        @NotNull String path,
                        @NotNull ContentType contentType)
      throws AzureCmdException {
    HttpsURLConnection sslConnection = manager.getSSLConnection(managementUrl, path, contentType);
    sslConnection.addRequestProperty(AUTHORIZATION_HEADER, "Bearer " + accessToken);
    return sslConnection;
  }
};

相关文章

HttpsURLConnection类方法