本文整理了Java中com.google.api.client.http.HttpRequest.setHeaders()
方法的一些代码示例,展示了HttpRequest.setHeaders()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.setHeaders()
方法的具体详情如下:
包路径:com.google.api.client.http.HttpRequest
类名称:HttpRequest
方法名:setHeaders
暂无
代码示例来源:origin: google/data-transfer-project
private void delete(String url) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept("text/turtle");
headers.setCookie(authCookie);
try {
HttpRequest deleteRequest = factory.buildDeleteRequest(new GenericUrl(url))
.setThrowExceptionOnExecuteError(false);
deleteRequest.setHeaders(headers);
validateResponse(deleteRequest.execute(), 200);
logger.debug("Deleted: {}", url);
} catch (IOException e) {
throw new IllegalStateException("Couldn't delete: " + url, e);
}
}
代码示例来源:origin: google/data-transfer-project
private String requestRaw(String path) throws IOException {
HttpRequest getRequest = TRANSPORT.createRequestFactory().buildGetRequest(
new GenericUrl(baseUrl + path));
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("Bearer " + accessToken);
getRequest.setHeaders(headers);
HttpResponse response = getRequest.execute();
validateResponse(getRequest, response, 200);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(response.getContent(), byteArrayOutputStream, true);
return byteArrayOutputStream.toString();
}
代码示例来源:origin: google/data-transfer-project
/**
* Parses the contents of a URL to produce an RDF model.
*/
public Model getModel(String url) throws IOException {
HttpRequestFactory factory = TRANSPORT.createRequestFactory();
HttpRequest rootGetRequest = factory.buildGetRequest(
new GenericUrl(url));
HttpHeaders headers = new HttpHeaders();
headers.setCookie(authCookie);
headers.setAccept("text/turtle");
rootGetRequest.setHeaders(headers);
HttpResponse response = rootGetRequest.execute();
if (response.getStatusCode() != 200) {
throw new IOException("Unexpected return code: "
+ response.getStatusCode()
+ "\nMessage:\n"
+ response.getStatusMessage());
}
StringWriter writer = new StringWriter();
IOUtils.copy(response.getContent(), writer, "UTF-8");
String fixedString = fixProblematicPeriods(writer.toString());
Model defaultModel = ModelFactory.createDefaultModel();
return defaultModel.read(
new StringReader(fixedString),
url,
"TURTLE");
}
代码示例来源:origin: google/data-transfer-project
/** Posts an RDF model to a Solid server. **/
public String postContent(
String url,
String slug,
String type,
Model model)
throws IOException {
StringWriter stringWriter = new StringWriter();
model.write(stringWriter, "TURTLE");
HttpContent content = new ByteArrayContent("text/turtle", stringWriter.toString().getBytes());
HttpRequest postRequest = factory.buildPostRequest(
new GenericUrl(url), content);
HttpHeaders headers = new HttpHeaders();
headers.setCookie(authCookie);
headers.set("Link", "<" + type + ">; rel=\"type\"");
headers.set("Slug", slug);
postRequest.setHeaders(headers);
HttpResponse response = postRequest.execute();
validateResponse(response, 201);
return response.getHeaders().getLocation();
}
代码示例来源:origin: GoogleContainerTools/jib
/**
* Sends the request.
*
* @param httpMethod the HTTP request method
* @param request the request to send
* @return the response to the sent request
* @throws IOException if building the HTTP request fails.
*/
public Response send(String httpMethod, Request request) throws IOException {
Preconditions.checkState(httpResponse == null, "Connection can send only one request");
HttpRequest httpRequest =
requestFactory
.buildRequest(httpMethod, url, request.getHttpContent())
.setHeaders(request.getHeaders());
if (request.getHttpTimeout() != null) {
httpRequest.setConnectTimeout(request.getHttpTimeout());
httpRequest.setReadTimeout(request.getHttpTimeout());
}
httpResponse = httpRequest.execute();
return new Response(httpResponse);
}
}
代码示例来源:origin: google/data-transfer-project
/** Posts a new status for the user, initially marked as private.**/
public void postStatus(String content, String idempotencyKey) throws IOException {
ImmutableMap<String, String> formParams = ImmutableMap.of(
"status", content,
// Default everything to private to avoid a privacy incident
"visibility", "private"
);
UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formParams);
HttpRequest postRequest = TRANSPORT.createRequestFactory()
.buildPostRequest(
new GenericUrl(baseUrl + POST_URL),
urlEncodedContent)
.setThrowExceptionOnExecuteError(false);
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("Bearer " + accessToken);
if (!Strings.isNullOrEmpty(idempotencyKey)) {
// This prevents the same post from being posted twice in the case of network errors
headers.set("Idempotency-Key", idempotencyKey);
}
postRequest.setHeaders(headers);
HttpResponse response = postRequest.execute();
validateResponse(postRequest, response, 200);
}
代码示例来源:origin: googleapis/google-cloud-java
private static String getAppEngineProjectIdFromMetadataServer() throws IOException {
String metadata = "http://metadata.google.internal";
String projectIdURL = "/computeMetadata/v1/project/project-id";
GenericUrl url = new GenericUrl(metadata + projectIdURL);
HttpTransport netHttpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory();
HttpRequest request =
requestFactory
.buildGetRequest(url)
.setConnectTimeout(500)
.setReadTimeout(500)
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
HttpResponse response = request.execute();
return headerContainsMetadataFlavor(response) ? response.parseAsString() : null;
}
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
HttpResponse response = request.execute();
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
.createRequestFactory()
.buildRequest(request.getRequestMethod(), request.getUrl(), request.getContent())
.setHeaders(httpHeaders);
代码示例来源:origin: GoogleCloudPlatform/java-docs-samples
request.setHeaders(new HttpHeaders().set("X-HTTP-Method-Override", "PATCH"));
HttpResponse response = request.execute();
代码示例来源:origin: GoogleCloudPlatform/google-cloud-intellij
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
HttpHeaders headers = new HttpHeaders();
httpRequest.setConnectTimeout(timeout);
httpRequest.setReadTimeout(timeout);
httpRequest.setHeaders(headers);
credential.initialize(httpRequest);
}
};
代码示例来源:origin: k0kubun/gitstar-ranking
private JsonObject graphql(String query)
throws IOException
{
String payload = Json.createObjectBuilder()
.add("query", query)
.add("variables", "{}")
.build().toString();
HttpRequest request = requestFactory.buildPostRequest(
new GenericUrl(GRAPHQL_ENDPOINT),
ByteArrayContent.fromString("application/json", payload));
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("bearer " + tokenFactory.getToken());
request.setHeaders(headers);
HttpResponse response = executeWithRetry(request);
// TODO: Handle error status code
JsonObject responseObject = Json.createReader(new StringReader(response.parseAsString())).readObject();
if (responseObject.containsKey("errors")) {
LOG.debug("errors with query:\n" + query);
LOG.debug("response:\n" + responseObject.toString());
}
return responseObject;
}
代码示例来源:origin: UglyTroLL/Douban-Java-SDK-OAuth2
@Override
public void initialize(HttpRequest hr) throws IOException {
hr.setParser(new XmlObjectParser(DefaultConfigs.DOUBAN_XML_NAMESPACE));
HttpHeaders header = new HttpHeaders();
header.setUserAgent("Dongxuexidu - Douban Java SDK");
hr.setHeaders(header);
hr.setNumberOfRetries(3);
}
});
代码示例来源:origin: com.infotel.seleniumRobot/core
protected static String uploadFile(String targetAppPath, String serverURL, String testDroidApiKey) throws IOException {
final HttpHeaders headers = new HttpHeaders().setBasicAuthentication(testDroidApiKey, "");
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(request -> {
request.setParser(new JsonObjectParser(JSON_FACTORY));
request.setHeaders(headers);
});
MultipartFormDataContent multipartContent = new MultipartFormDataContent();
FileContent fileContent = new FileContent("application/octet-stream", new File(targetAppPath));
MultipartFormDataContent.Part filePart = new MultipartFormDataContent.Part("file", fileContent);
multipartContent.addPart(filePart);
HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(serverURL+"/upload"), multipartContent);
AppiumResponse appiumResponse = request.execute().parseAs(AppiumResponse.class);
logger.info("File id:" + appiumResponse.uploadStatus.fileInfo.file);
return appiumResponse.uploadStatus.fileInfo.file;
}
代码示例来源:origin: com.google.api-ads/ads-lib
/**
* Gets the report HTTP URL connection given report URL and proper information needed to
* authenticate the request.
*
* @param reportUrl the URL of the report response or download
* @return the report HTTP URL connection
* @throws AuthenticationException If OAuth authorization fails.
*/
@VisibleForTesting
HttpRequestFactory getHttpRequestFactory(final String reportUrl, String version)
throws AuthenticationException {
final HttpHeaders httpHeaders = createHeaders(reportUrl, version);
return httpTransport.createRequestFactory(
request -> {
request.setHeaders(httpHeaders);
request.setConnectTimeout(reportDownloadTimeout);
request.setReadTimeout(reportDownloadTimeout);
request.setThrowExceptionOnExecuteError(false);
request.setLoggingEnabled(true);
request.setResponseInterceptor(responseInterceptor);
});
}
代码示例来源:origin: googleapis/gax-java
@Override
public void initialize(HttpRequest request) throws IOException {
request.setHeaders(
new HttpHeaders().set("headerkey1", headerValue1).set("headerkey2", headerValue2));
}
};
代码示例来源:origin: se.redmind/rmtest-appium
public static String uploadFile(String appPath, String serverURL, String username, String password) throws IOException {
HttpHeaders headers = new HttpHeaders();
if (username != null) {
headers.setBasicAuthentication(username, password);
}
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(request -> {
request.setParser(new JsonObjectParser(new JacksonFactory()));
request.setHeaders(headers);
});
MultipartContent multipartContent = new MultipartContent();
FileContent fileContent = new FileContent("application/octet-stream", new File(appPath));
MultipartContent.Part filePart = new MultipartContent.Part(new HttpHeaders()
.set("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileContent.getFile().getName() + "\""), fileContent);
multipartContent.addPart(filePart);
HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(serverURL), multipartContent);
HttpResponse response = request.execute();
LoggerFactory.getLogger(AppiumDriverWrapper.class).info("response:" + response.parseAsString());
AppiumResponse appiumResponse = request.execute().parseAs(AppiumResponse.class);
LoggerFactory.getLogger(AppiumDriverWrapper.class).info("File id:" + appiumResponse.uploadStatus.fileInfo.file);
return appiumResponse.uploadStatus.fileInfo.file;
}
代码示例来源:origin: k0kubun/gitstar-ranking
public List<User> getUsersSince(int since)
throws IOException
{
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(API_ENDPOINT + "/users?since=" + since));
HttpHeaders headers = new HttpHeaders();
headers.setAuthorization("bearer " + tokenFactory.getToken());
request.setHeaders(headers);
HttpResponse response = executeWithRetry(request);
// TODO: Handle error status code
List<JsonObject> userObjects = Json.createReader(new StringReader(response.parseAsString())).readArray().getValuesAs(JsonObject.class);
List<User> users = new ArrayList<>();
for (JsonObject userObject : userObjects) {
User user = new User(userObject.getInt("id"), userObject.getString("type"));
user.setLogin(userObject.getString("login"));
user.setAvatarUrl(userObject.getString("avatar_url"));
users.add(user);
}
return users;
}
代码示例来源:origin: gradle.plugin.com.google.cloud.tools/jib-gradle-plugin
/**
* Sends the request.
*
* @param httpMethod the HTTP request method
* @param request the request to send
* @return the response to the sent request
* @throws IOException if building the HTTP request fails.
*/
public Response send(String httpMethod, Request request) throws IOException {
Preconditions.checkState(httpResponse == null, "Connection can send only one request");
HttpRequest httpRequest =
requestFactory
.buildRequest(httpMethod, url, request.getHttpContent())
.setHeaders(request.getHeaders());
if (request.getHttpTimeout() != null) {
httpRequest.setConnectTimeout(request.getHttpTimeout());
httpRequest.setReadTimeout(request.getHttpTimeout());
}
httpResponse = httpRequest.execute();
return new Response(httpResponse);
}
}
代码示例来源:origin: com.google.cloud/google-cloud-core
private static String getAppEngineProjectIdFromMetadataServer() throws IOException {
String metadata = "http://metadata.google.internal";
String projectIdURL = "/computeMetadata/v1/project/project-id";
GenericUrl url = new GenericUrl(metadata + projectIdURL);
HttpTransport netHttpTransport = new NetHttpTransport();
HttpRequestFactory requestFactory = netHttpTransport.createRequestFactory();
HttpRequest request =
requestFactory
.buildGetRequest(url)
.setConnectTimeout(500)
.setReadTimeout(500)
.setHeaders(new HttpHeaders().set("Metadata-Flavor", "Google"));
HttpResponse response = request.execute();
return headerContainsMetadataFlavor(response) ? response.parseAsString() : null;
}
内容来源于网络,如有侵权,请联系作者删除!