com.google.appengine.api.urlfetch.HTTPRequest.setHeader()方法的使用及代码示例

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

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

HTTPRequest.setHeader介绍

暂无

代码示例

代码示例来源:origin: googlemaps/google-maps-services-java

@Override
public <T, R extends ApiResponse<T>> PendingResult<T> handlePost(
  String hostName,
  String url,
  String payload,
  String userAgent,
  Class<R> clazz,
  FieldNamingPolicy fieldNamingPolicy,
  long errorTimeout,
  Integer maxRetries,
  ExceptionsAllowedToRetry exceptionsAllowedToRetry) {
 FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10);
 HTTPRequest req = null;
 try {
  req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions);
  req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8"));
  req.setPayload(payload.getBytes(UTF_8));
 } catch (MalformedURLException e) {
  LOG.error("Request: {}{}", hostName, url, e);
  throw (new RuntimeException(e));
 }
 return new GaePendingResult<>(
   req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry);
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

HTTPRequest createPutRequest(final GcsRestCreationToken token, final ByteBuffer chunk,
  final boolean isFinalChunk, long timeoutMillis, final int length) {
 long offset = token.offset;
 Preconditions.checkArgument(offset % CHUNK_ALIGNMENT_BYTES == 0,
   "%s: Offset not aligned; offset=%s, length=%s, token=%s",
   this, offset, length, token);
 Preconditions.checkArgument(isFinalChunk || length % CHUNK_ALIGNMENT_BYTES == 0,
   "%s: Chunk not final and not aligned: offset=%s, length=%s, token=%s",
   this, offset, length, token);
 Preconditions.checkArgument(isFinalChunk || length > 0,
   "%s: Chunk empty and not final: offset=%s, length=%s, token=%s",
   this, offset, length, token);
 if (log.isLoggable(Level.FINEST)) {
  log.finest(this + ": About to write to " + token + " " + String.format("0x%x", length)
    + " bytes at offset " + String.format("0x%x", offset)
    + "; isFinalChunk: " + isFinalChunk + ")");
 }
 long limit = offset + length;
 Map<String, String> queryStrings = Collections.singletonMap(UPLOAD_ID, token.uploadId);
 final HTTPRequest req =
   makeRequest(token.filename, queryStrings, PUT, timeoutMillis, chunk);
 req.setHeader(
   new HTTPHeader(CONTENT_RANGE,
     "bytes " + (length == 0 ? "*" : offset + "-" + (limit - 1))
     + (isFinalChunk ? "/" + limit : "/*")));
 return req;
}

代码示例来源:origin: com.threewks.thundr/thundr-gae

private void setContentTypeIfNotPresent(HttpRequestImpl request, HTTPRequest fetchRequest, ContentType contentType) {
  if (!request.getHeaders().containsKey(Header.ContentType)) {
    fetchRequest.setHeader(new HTTPHeader(Header.ContentType, contentType.value()));
  }
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

private HTTPRequest createAuthorizeRequest(final HTTPRequest req) throws RetryHelperException {
 String token = RetryHelper.runWithRetries(new Callable<String>() {
  @Override
  public String call() {
   return getToken();
  }
 }, RETRY_PARAMS, EXCEPTION_HANDLER);
 HTTPRequest request = URLFetchUtils.copyRequest(req);
 request.setHeader(new HTTPHeader("Authorization", "Bearer " + token));
 return request;
}

代码示例来源:origin: com.google.maps/google-maps-services

@Override
public <T, R extends ApiResponse<T>> PendingResult<T> handlePost(
  String hostName,
  String url,
  String payload,
  String userAgent,
  Class<R> clazz,
  FieldNamingPolicy fieldNamingPolicy,
  long errorTimeout,
  Integer maxRetries,
  ExceptionsAllowedToRetry exceptionsAllowedToRetry) {
 FetchOptions fetchOptions = FetchOptions.Builder.withDeadline(10);
 HTTPRequest req = null;
 try {
  req = new HTTPRequest(new URL(hostName + url), HTTPMethod.POST, fetchOptions);
  req.setHeader(new HTTPHeader("Content-Type", "application/json; charset=utf-8"));
  req.setPayload(payload.getBytes(UTF_8));
 } catch (MalformedURLException e) {
  LOG.error("Request: {}{}", hostName, url, e);
  throw (new RuntimeException(e));
 }
 return new GaePendingResult<>(
   req, client, clazz, fieldNamingPolicy, errorTimeout, maxRetries, exceptionsAllowedToRetry);
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

private void addOptionsHeaders(HTTPRequest req, GcsFileOptions options) {
 if (options == null) {
  return;
 }
 if (options.getMimeType() != null) {
  req.setHeader(new HTTPHeader(CONTENT_TYPE, options.getMimeType()));
 }
 if (options.getAcl() != null) {
  req.setHeader(new HTTPHeader(ACL, options.getAcl()));
 }
 if (options.getCacheControl() != null) {
  req.setHeader(new HTTPHeader(CACHE_CONTROL, options.getCacheControl()));
 }
 if (options.getContentDisposition() != null) {
  req.setHeader(new HTTPHeader(CONTENT_DISPOSITION, options.getContentDisposition()));
 }
 if (options.getContentEncoding() != null) {
  req.setHeader(new HTTPHeader(CONTENT_ENCODING, options.getContentEncoding()));
 }
 for (Entry<String, String> entry : options.getUserMetadata().entrySet()) {
  req.setHeader(new HTTPHeader(X_GOOG_META + entry.getKey(), entry.getValue()));
 }
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

@VisibleForTesting
HTTPRequest makeRequest(GcsFilename filename, @Nullable Map<String, String> queryStrings,
  HTTPMethod method, long timeoutMillis, byte[] payload) {
 HTTPRequest request = new HTTPRequest(makeUrl(filename, queryStrings), method,
   FetchOptions.Builder.disallowTruncate()
     .doNotFollowRedirects()
     .validateCertificate()
     .setDeadline(timeoutMillis / 1000.0));
 for (HTTPHeader header : headers) {
  request.addHeader(header);
 }
 request.addHeader(USER_AGENT);
 if (payload != null && payload.length > 0) {
  request.setHeader(new HTTPHeader(CONTENT_LENGTH, String.valueOf(payload.length)));
  try {
   request.setHeader(new HTTPHeader(CONTENT_MD5,
     BaseEncoding.base64().encode(MessageDigest.getInstance("MD5").digest(payload))));
  } catch (NoSuchAlgorithmException e) {
   log.severe(
     "Unable to get a MessageDigest instance, no Content-MD5 header sent.\n" + e.toString());
  }
  request.setPayload(payload);
 } else {
  request.setHeader(ZERO_CONTENT_LENGTH);
 }
 return request;
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

req.setHeader(
  new HTTPHeader(RANGE, "bytes=" + startOffsetBytes + "-" + (startOffsetBytes + want - 1)));
final HTTPRequestInfo info = new HTTPRequestInfo(req);

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

@Override
public RawGcsCreationToken beginObjectCreation(
  GcsFilename filename, GcsFileOptions options, long timeoutMillis) throws IOException {
 HTTPRequest req = makeRequest(filename, null, POST, timeoutMillis);
 req.setHeader(RESUMABLE_HEADER);
 addOptionsHeaders(req, options);
 HTTPResponse resp;
 try {
  resp = urlfetch.fetch(req);
 } catch (IOException e) {
  throw createIOException(new HTTPRequestInfo(req), e);
 }
 if (resp.getResponseCode() == 201) {
  String location = URLFetchUtils.getSingleHeader(resp, LOCATION);
  String queryString = new URL(location).getQuery();
  Preconditions.checkState(
    queryString != null, LOCATION + " header," + location + ", witout a query string");
  Map<String, String> params = Splitter.on('&').withKeyValueSeparator('=').split(queryString);
  Preconditions.checkState(params.containsKey(UPLOAD_ID),
    LOCATION + " header," + location + ", has a query string without " + UPLOAD_ID);
  return new GcsRestCreationToken(filename, params.get(UPLOAD_ID), 0);
 } else {
  throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
 }
}

代码示例来源:origin: com.threewks.thundr/thundr-gae

private void addBody(HttpRequestImpl request, HTTPRequest fetchRequest, boolean createFormPostIfBodyIsEmpty) {
  byte[] data = null;
  try {
    Object body = request.getBody();
    if (body == null && createFormPostIfBodyIsEmpty) {
      body = createFormPostBody(request);
    }
    if (isMultipart(request)) {
      HttpEntity multipart = prepareMultipart(request);
      ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
      multipart.writeTo(baos);
      data = baos.toByteArray();
      fetchRequest.setHeader(new HTTPHeader(Header.ContentType, multipart.getContentType().getValue()));
    } else if (body != null) {
      InputStream is = convertOutgoing(body);
      data = Streams.readBytes(is);
    }
  } catch (Exception e) {
    throw new HttpException(e, "Failed to generate request body: %s", e.getMessage());
  }
  if (data != null) {
    fetchRequest.setPayload(data);
  }
}

代码示例来源:origin: GoogleCloudPlatform/appengine-gcs-client

@Override
public void copyObject(GcsFilename source, GcsFilename dest, GcsFileOptions fileOptions,
  long timeoutMillis) throws IOException {
 HTTPRequest req = makeRequest(dest, null, PUT, timeoutMillis);
 req.setHeader(new HTTPHeader(X_GOOG_COPY_SOURCE, makePath(source)));
 if (fileOptions != null) {
  req.setHeader(REPLACE_METADATA_HEADER);
  addOptionsHeaders(req, fileOptions);
 }
 HTTPResponse resp;
 try {
  resp = urlfetch.fetch(req);
 } catch (IOException e) {
  throw createIOException(new HTTPRequestInfo(req), e);
 }
 if (resp.getResponseCode() != 200) {
  throw HttpErrorHandler.error(new HTTPRequestInfo(req), resp);
 }
}

代码示例来源:origin: jclouds/legacy-jclouds

gaeRequest.setHeader(new HTTPHeader("Expect", "100-continue"));
  request.getPayload().getContentMetadata()).entries()) {
if (!prohibitedHeaders.contains(header.getKey()))
  gaeRequest.setHeader(new HTTPHeader(header.getKey(), header.getValue()));

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

gaeRequest.setHeader(new HTTPHeader("Expect", "100-continue"));
  request.getPayload().getContentMetadata()).entries()) {
if (!prohibitedHeaders.contains(header.getKey()))
  gaeRequest.setHeader(new HTTPHeader(header.getKey(), header.getValue()));

代码示例来源:origin: GoogleCloudPlatform/appengine-tck

@Test
public void testPayload() throws Exception {
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  URL url = getFetchUrl();
  HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
  req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
  req.setPayload("Tralala".getBytes(UTF_8));
  HTTPResponse response = service.fetch(req);
  String content = new String(response.getContent());
  Assert.assertEquals("Hopsasa", content);
}

代码示例来源:origin: GoogleCloudPlatform/appengine-tck

@Test
public void testHeaders() throws Exception {
  URLFetchService service = URLFetchServiceFactory.getURLFetchService();
  URL url = getFetchUrl();
  HTTPRequest req = new HTTPRequest(url, HTTPMethod.POST);
  req.setHeader(new HTTPHeader("Content-Type", "application/octet-stream"));
  req.setPayload("Headers!".getBytes(UTF_8));
  HTTPResponse response = service.fetch(req);
  boolean found = false;
  List<HTTPHeader> headers = response.getHeadersUncombined();
  for (HTTPHeader h : headers) {
    if (h.getName().equals("ABC")) {
      Assert.assertEquals("123", h.getValue());
      found = true;
      break;
    }
  }
  Assert.assertTrue("Cannot find matching header <ABC : 123>: " + headers, found);
  found = false;
  headers = response.getHeaders();
  for (HTTPHeader h : headers) {
    if (h.getName().equals("XYZ")) {
      Assert.assertEquals("1, 2, 3", h.getValue());
      found = true;
      break;
    }
  }
  Assert.assertTrue("Cannot find matching header <XYZ : 1,2,3>: " + headers, found);
}

相关文章