java.net.HttpURLConnection.addRequestProperty()方法的使用及代码示例

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

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

HttpURLConnection.addRequestProperty介绍

暂无

代码示例

代码示例来源:origin: jenkinsci/jenkins

private URL tryToResolveRedirects(URL base, String authorization) {
  try {
    HttpURLConnection con = (HttpURLConnection) base.openConnection();
    if (authorization != null) {
      con.addRequestProperty("Authorization", authorization);
    }
    con.getInputStream().close();
    base = con.getURL();
  } catch (Exception ex) {
    // Do not obscure the problem propagating the exception. If the problem is real it will manifest during the
    // actual exchange so will be reported properly there. If it is not real (no permission in UI but sufficient
    // for CLI connection using one of its mechanisms), there is no reason to bother user about it.
    LOGGER.log(Level.FINE, "Failed to resolve potential redirects", ex);
  }
  return base;
}

代码示例来源:origin: aws/aws-sdk-java

public HttpURLConnection connectToEndpoint(URI endpoint, Map<String, String> headers) throws IOException {
  HttpURLConnection connection = (HttpURLConnection) endpoint.toURL().openConnection(Proxy.NO_PROXY);
  connection.setConnectTimeout(1000 * 2);
  connection.setReadTimeout(1000 * 5);
  connection.setRequestMethod("GET");
  connection.setDoOutput(true);
  for (Map.Entry<String, String> header : headers.entrySet()) {
    connection.addRequestProperty(header.getKey(), header.getValue());
  }
  // TODO should we autoredirect 3xx
  // connection.setInstanceFollowRedirects(false);
  connection.connect();
  return connection;
}

代码示例来源:origin: spotify/helios

private HttpURLConnection connect(final URI uri, final Map<String, List<String>> headers)
   throws IOException {
  final HttpURLConnection connection;
  connection = (HttpURLConnection) uri.toURL().openConnection();
  connection.setInstanceFollowRedirects(false);
  for (final Map.Entry<String, List<String>> header : headers.entrySet()) {
   for (final String value : header.getValue()) {
    connection.addRequestProperty(header.getKey(), value);
   }
  }
  connection.setRequestMethod("GET");
  connection.getResponseCode();
  return connection;
 }
}

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

public ConnBuilder(Credentials credentials, String container, String object) {
  try {
    String url = credentials.storageURL + "/" + container;
    if (object != null) {
      url = url + "/" + object;
    }
    con = (HttpURLConnection) new URL(url).openConnection();
    con.addRequestProperty(STORAGE_TOKEN_HEADER, credentials.authToken);
    con.addRequestProperty(ACCEPT_HEADER, "*/*");
  } catch (IOException e) {
    log.error(Util.getMessage("ErrorCreatingConnection"), e);
  }
}

代码示例来源:origin: jenkinsci/jenkins

HttpURLConnection con = (HttpURLConnection) target.openConnection();
con.setDoOutput(true); // request POST to avoid caching
con.setRequestMethod("POST");
con.addRequestProperty("Session", uuid.toString());
con.addRequestProperty("Side","download");
if (authorization != null) {
  con.addRequestProperty("Authorization", authorization);
con = (HttpURLConnection) target.openConnection();
con.setDoOutput(true); // request POST
con.setRequestMethod("POST");
con.setChunkedStreamingMode(0);
con.setRequestProperty("Content-type","application/octet-stream");
con.addRequestProperty("Session", uuid.toString());
con.addRequestProperty("Side","upload");
if (authorization != null) {
  con.addRequestProperty ("Authorization", authorization);

代码示例来源:origin: alibaba/nacos

static public HttpResult invokeURL(String url, List<String> headers, String encoding) throws IOException {
  HttpURLConnection conn = null;
  try {
    conn = (HttpURLConnection)new URL(url).openConnection();
        conn.addRequestProperty(iter.next(), iter.next());
    conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + encoding);

代码示例来源:origin: Alluxio/alluxio

/**
  * Swift HTTP PUT request.
  *
  * @param access JOSS access object
  * @param objectName name of the object to create
  * @return SwiftOutputStream that will be used to upload data to Swift
  */
 public static SwiftOutputStream put(Access access, String objectName) throws IOException {
  LOG.debug("PUT method, object : {}", objectName);
  URL url = new URL(access.getPublicURL() + "/" + objectName);
  URLConnection connection = url.openConnection();
  if (!(connection instanceof HttpURLConnection)) {
   throw new IOException("Connection is not an instance of HTTP URL Connection");
  }

  HttpURLConnection httpCon = (HttpURLConnection) connection;
  httpCon.setRequestMethod("PUT");
  httpCon.addRequestProperty("X-Auth-Token", access.getToken());
  httpCon.addRequestProperty("Content-Type", "binary/octet-stream");
  httpCon.setDoInput(true);
  httpCon.setRequestProperty("Connection", "close");
  httpCon.setReadTimeout(HTTP_READ_TIMEOUT);
  httpCon.setRequestProperty("Transfer-Encoding", "chunked");
  httpCon.setDoOutput(true);
  httpCon.setChunkedStreamingMode(HTTP_CHUNK_STREAMING);
  httpCon.connect();
  return new SwiftOutputStream(httpCon);
 }
}

代码示例来源:origin: alibaba/nacos

public static HttpResult httpGetWithTimeOut(String url, List<String> headers, Map<String, String> paramValues, int connectTimeout, int readTimeout, String encoding) {
  HttpURLConnection conn = null;
  try {
    String encodedContent = encodingParams(paramValues, encoding);
    url += (null == encodedContent) ? "" : ("?" + encodedContent);
    conn = (HttpURLConnection) new URL(url).openConnection();
    conn.setConnectTimeout(connectTimeout);
    conn.setReadTimeout(readTimeout);
    conn.setRequestMethod("GET");
    conn.addRequestProperty("Client-Version", UtilsAndCommons.SERVER_VERSION);
    setHeaders(conn, headers, encoding);
    conn.connect();
    return getResult(conn);
  } catch (Exception e) {
    Loggers.SRV_LOG.warn("[VIPSRV] Exception while request: {}, caused: {}", url, e);
    return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
  } finally {
    if (conn != null) {
      conn.disconnect();
    }
  }
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Connects to the metadata service to read the specified resource and
 * returns the text contents.
 *
 * @param resourcePath
 *            The resource
 *
 * @return The text payload returned from the Amazon EC2 Instance Metadata
 *         service for the specified resource path.
 *
 * @throws IOException
 *             If any problems were encountered while connecting to metadata
 *             service for the requested resource path.
 * @throws SdkClientException
 *             If the requested metadata service is not found.
 */
public String readResource(String resourcePath) throws IOException, SdkClientException {
  URL url = getEc2MetadataServiceUrlForResource(resourcePath);
  log.debug("Connecting to EC2 instance metadata service at URL: " + url.toString());
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setConnectTimeout(1000 * 2);
  connection.setReadTimeout(1000 * 5);
  connection.setRequestMethod("GET");
  connection.setDoOutput(true);
  connection.addRequestProperty("User-Agent", USER_AGENT);
  connection.connect();
  return readResponse(connection);
}

代码示例来源:origin: spotify/helios

private HttpURLConnection connect0(final URI ipUri, final String method, final byte[] entity,
                  final Map<String, List<String>> headers,
                  final String endpointHost)
  throws IOException {
 if (log.isTraceEnabled()) {
  log.trace("req: {} {} {} {} {} {}", method, ipUri, headers.size(),
    Joiner.on(',').withKeyValueSeparator("=").join(headers),
    entity.length, Json.asPrettyStringUnchecked(entity));
 } else {
  log.debug("req: {} {} {} {}", method, ipUri, headers.size(), entity.length);
 }
 final HttpURLConnection connection = (HttpURLConnection) ipUri.toURL().openConnection();
 handleHttps(connection, endpointHost, hostnameVerifierProvider, extraHttpsHandler);
 connection.setRequestProperty("Accept-Encoding", "gzip");
 connection.setInstanceFollowRedirects(false);
 connection.setConnectTimeout(httpTimeoutMillis);
 connection.setReadTimeout(httpTimeoutMillis);
 for (final Map.Entry<String, List<String>> header : headers.entrySet()) {
  for (final String value : header.getValue()) {
   connection.addRequestProperty(header.getKey(), value);
  }
 }
 if (entity.length > 0) {
  connection.setDoOutput(true);
  connection.getOutputStream().write(entity);
 }
 setRequestMethod(connection, method, connection instanceof HttpsURLConnection);
 return connection;
}

代码示例来源:origin: signalapp/Signal-Server

logger.debug("Reporting metrics...");
URL url = new URL("https", hostname, 443, String.format("/report/metrics?t=%s&h=%s", token, host));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");

代码示例来源:origin: jiangqqlmj/FastDev4Android

try {
  URL url = new URL(baseUrl);
  urlConn = (HttpURLConnection) url.openConnection();
  urlConn.addRequestProperty("Accept-Encoding", "gzip, deflate");
  urlConn.setRequestMethod(method);
  urlConn.setDoOutput(true);

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

final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.addRequestProperty(header.getKey(), header.getValue());

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

final HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.addRequestProperty(header.getKey(), header.getValue());

代码示例来源: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 (url.getPort() > 0 && url.getPort() != 80)
    host += ":" + url.getPort();
  conn.addRequestProperty("Host", host);

代码示例来源:origin: AltBeacon/android-beacon-library

} else {
  try {
    conn = (HttpURLConnection) url.openConnection();
    conn.addRequestProperty("User-Agent", mUserAgentString);
    mResponseCode = conn.getResponseCode();
    LogManager.d(TAG, "response code is %s", conn.getResponseCode());

代码示例来源: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())
  );
    conn.setDoOutput(true);
  if (req.cookies().size() > 0)
    conn.addRequestProperty("Cookie", getRequestCookieString(req));
  for (Map.Entry<String, List<String>> header : req.multiHeaders().entrySet()) {
    for (String value : header.getValue()) {
      conn.addRequestProperty(header.getKey(), value);

代码示例来源:origin: commons-validator/commons-validator

modTime = 0;
HttpURLConnection hc = (HttpURLConnection) new URL(tldurl).openConnection();
if (modTime > 0) {
  SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");//Sun, 06 Nov 1994 08:49:37 GMT
  String since = sdf.format(new Date(modTime));
  hc.addRequestProperty("If-Modified-Since", since);
  System.out.println("Found " + f + " with date " + since);

代码示例来源:origin: com.amazonaws/aws-java-sdk-core

public HttpURLConnection connectToEndpoint(URI endpoint, Map<String, String> headers) throws IOException {
  HttpURLConnection connection = (HttpURLConnection) endpoint.toURL().openConnection(Proxy.NO_PROXY);
  connection.setConnectTimeout(1000 * 2);
  connection.setReadTimeout(1000 * 5);
  connection.setRequestMethod("GET");
  connection.setDoOutput(true);
  for (Map.Entry<String, String> header : headers.entrySet()) {
    connection.addRequestProperty(header.getKey(), header.getValue());
  }
  // TODO should we autoredirect 3xx
  // connection.setInstanceFollowRedirects(false);
  connection.connect();
  return connection;
}

代码示例来源:origin: com.amazonaws/aws-java-sdk-core

/**
 * Connects to the metadata service to read the specified resource and
 * returns the text contents.
 *
 * @param resourcePath
 *            The resource
 *
 * @return The text payload returned from the Amazon EC2 Instance Metadata
 *         service for the specified resource path.
 *
 * @throws IOException
 *             If any problems were encountered while connecting to metadata
 *             service for the requested resource path.
 * @throws SdkClientException
 *             If the requested metadata service is not found.
 */
public String readResource(String resourcePath) throws IOException, SdkClientException {
  URL url = getEc2MetadataServiceUrlForResource(resourcePath);
  log.debug("Connecting to EC2 instance metadata service at URL: " + url.toString());
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setConnectTimeout(1000 * 2);
  connection.setReadTimeout(1000 * 5);
  connection.setRequestMethod("GET");
  connection.setDoOutput(true);
  connection.addRequestProperty("User-Agent", USER_AGENT);
  connection.connect();
  return readResponse(connection);
}

相关文章

HttpURLConnection类方法