本文整理汇总了Java中org.apache.http.client.methods.HttpHead.setHeader方法的典型用法代码示例。如果您正苦于以下问题:Java HttpHead.setHeader方法的具体用法?Java HttpHead.setHeader怎么用?Java HttpHead.setHeader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.HttpHead
的用法示例。
在下文中一共展示了HttpHead.setHeader方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: head
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
/**
* HEAD request
* @param url URL to make request to
* @param headers Extra headers to add. Even-numbered elements will be treated as header names, and odd-numbered elements will be treated as header values.
* @return Response status code.
* @throws java.io.IOException
* @throws java.security.GeneralSecurityException
*/
public static String head(String url, String... headers) throws IOException, GeneralSecurityException {
try (CloseableHttpClient client = getClient()) {
HttpHead request = new HttpHead(url);
// Set request headers
if(headers != null) {
for(int i = 0; i < headers.length; i+=2) {
request.setHeader(headers[i], headers[i+1]);
}
}
// Execute the request
try (CloseableHttpResponse response = client.execute(request)) {
// Parse the response
int statusCode = response.getStatusLine().getStatusCode();
// Package it up for MATLAB.
String returnVal = Integer.toString(statusCode);
return returnVal;
}
}
}
示例2: generateHttpRequest
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
/**
* 构造HttpHead
*/
protected HttpUriRequest generateHttpRequest() {
String resource = httpTool.generateCanonicalizedResource("/"
+ bucketName + "/" + objectKey);
String requestUri = OSS_END_POINT + resource;
HttpHead httpHead = new HttpHead(requestUri);
String dateStr = Helper.getGMTDate();
String authorization = OSSHttpTool
.generateAuthorization(accessId, accessKey,
httpMethod.toString(), "", "", dateStr, "", resource);
httpHead.setHeader(AUTHORIZATION, authorization);
httpHead.setHeader(DATE, dateStr);
httpHead.setHeader(HOST, OSS_HOST);
OSSHttpTool.addHttpRequestHeader(httpHead, IF_MODIFIED_SINCE,
Helper.getGMTDate(modifiedSince));
OSSHttpTool.addHttpRequestHeader(httpHead, IF_UNMODIFIED_SINCE,
Helper.getGMTDate(unModifiedSince));
OSSHttpTool.addHttpRequestHeader(httpHead, IF_MATCH, expectedETag);
OSSHttpTool.addHttpRequestHeader(httpHead, IF_NONE_MATCH,
unexpectedETag);
return httpHead;
}
示例3: testHeadToGetSwitch
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
@Test
public void testHeadToGetSwitch() throws Exception {
HttpHead head = new HttpHead(getHttpURl("/hello/html"));
// When checking the content length, we must disable the compression:
head.setHeader(HeaderNames.ACCEPT_ENCODING, "identity");
HttpResponse<String> response;
try {
org.apache.http.HttpResponse resp = ClientFactory.getHttpClient().execute(head);
response = new HttpResponse<>(resp, String.class);
} finally {
head.releaseConnection();
}
assertThat(response.code()).isEqualTo(OK);
assertThat(response.contentType()).isEqualTo(MimeTypes.HTML);
System.out.println(response.headers());
assertThat(Integer.valueOf(response.header(CONTENT_LENGTH))).isEqualTo(20);
}
示例4: testHead
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
public void testHead(String resource, String host) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpHead httpMethod = new HttpHead(url + resource);
if (host != null && !host.trim().isEmpty())
httpMethod.setHeader("Host", host);
HttpResponse response = httpclient.execute(httpMethod);
System.out.println(httpMethod);
System.out.println(response.toString());
EntityUtils.consume(response.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
httpclient.close();
}
示例5: setPostsAsRead
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
public boolean setPostsAsRead(Topic topic, int pageNumber) throws DataRetrieverException
{
String url = POSTS_URL.replaceFirst("\\{\\$cat\\}", topic.getCategory().getRealId())
.replaceFirst("\\{\\$topic\\}", String.valueOf(topic.getId()))
.replaceFirst("\\{\\$page\\}", String.valueOf(pageNumber));
Log.d(HFR4droidApplication.TAG, "Retrieving " + url);
try
{
URI uri = new URI(url);
HttpHead method = new HttpHead(uri);
method.setHeader("User-Agent", HFR4droidApplication.getUserAgent());
HttpContext httpContext = new BasicHttpContext();
if (auth != null && auth.getCookies() != null && fakeCs != null)
{
httpContext.setAttribute(ClientContext.COOKIE_STORE, auth.getCookies());
}
HttpResponse response = httpClientHelper.getHttpClient().execute(method, httpContext);
Log.d(HFR4droidApplication.TAG, "Status : " + response.getStatusLine().getStatusCode() + ", " + response.getStatusLine().getReasonPhrase());
return response.getStatusLine().getStatusCode() == 200;
}
catch (Exception e)
{
throw new DataRetrieverException(context.getString(R.string.error_dr_topics), e);
}
}
示例6: headResponsObject
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
public static Map<String, String> headResponsObject(String urlOffset, OAuth2AccessToken oauth2AccessToken, Map<String, String> headers, int expect) throws CloudFoundryException {
HttpHead request = new HttpHead();
try {
request.setURI(new URL(oauth2AccessToken.getString(OAuth2AccessToken.Fields.target.name())+urlOffset).toURI());
} catch (MalformedURLException | JSONException | URISyntaxException e) {
e.printStackTrace();
throw new CloudFoundryException(HttpStatus.SC_BAD_REQUEST,e.getMessage());
}
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setHeader("Accept", "application/json;charset=utf-8");
request.setHeader("Authorization", "bearer "+oauth2AccessToken.getString(OAuth2AccessToken.Fields.access_token.name()));
if (headers!=null) {
for (String key : headers.keySet()) {
request.setHeader(key, headers.get(key));
}
}
try {
HttpResponse response = HttpClientFactory.getThreadSafeClient().execute(request);
int returnCode = response.getStatusLine().getStatusCode();
if ((returnCode!=HttpStatus.SC_OK)&&(returnCode!=HttpStatus.SC_PARTIAL_CONTENT)) {
throw new CloudFoundryException(response.getStatusLine().getStatusCode(),response.getStatusLine().getReasonPhrase());
}
// copy the headers into the response object in case caller needs them, some silly
// api contract thought this is a good way to communicate information.
Header[] rheaders = response.getAllHeaders();
headers = new HashMap<String,String>();
for (int i = 0; i < rheaders.length; i++) {
headers.put(rheaders[i].getName(), rheaders[i].getValue());
}
return headers;
}
catch (Throwable t) {
// t.printStackTrace();
throw new CloudFoundryException(HttpStatus.SC_BAD_REQUEST,t.getMessage());
}
}
示例7: getWorkspacePermissions
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
private String getWorkspacePermissions(User user, Workspace workspace) throws Exception {
if (!isTokenActive()) {
login();
}
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] certificate, String authType) {
return true;
}
};
SSLSocketFactory sf = new SSLSocketFactory(acceptingTrustStrategy, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 5000, sf));
ClientConnectionManager ccm = new SingleClientConnManager(registry);
HttpClient httpClient = new DefaultHttpClient(ccm);
String url = this.storageUrl + "/" + workspace.getSwiftContainer();
try {
HttpHead request = new HttpHead(url);
request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);
HttpResponse response = httpClient.execute(request);
SwiftResponse swiftResponse = new SwiftResponse(response);
if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
throw new UnauthorizedException("404 User unauthorized");
}
if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
}
// We suppose there are the same permissions for read and write
Header containerWriteHeader = swiftResponse.getResponseHeader(SwiftResponse.X_CONTAINER_WRITE);
if (containerWriteHeader == null) {
return "";
}
return containerWriteHeader.getValue();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
示例8: getWorkspacePermissions
import org.apache.http.client.methods.HttpHead; //导入方法依赖的package包/类
private String getWorkspacePermissions(User user, Workspace workspace) throws Exception {
if (!isTokenActive()) {
login();
}
HttpClient httpClient = new DefaultHttpClient();
String url = this.storageUrl + "/" + workspace.getSwiftContainer();
try {
HttpHead request = new HttpHead(url);
request.setHeader(SwiftResponse.X_AUTH_TOKEN, authToken);
HttpResponse response = httpClient.execute(request);
SwiftResponse swiftResponse = new SwiftResponse(response);
if (swiftResponse.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
throw new UnauthorizedException("404 User unauthorized");
}
if (swiftResponse.getStatusCode() < 200 || swiftResponse.getStatusCode() >= 300) {
throw new UnexpectedStatusCodeException("Unexpected status code: " + swiftResponse.getStatusCode());
}
// We suppose there are the same permissions for read and write
Header containerWriteHeader = swiftResponse.getResponseHeader(SwiftResponse.X_CONTAINER_WRITE);
if (containerWriteHeader == null) {
return "";
}
return containerWriteHeader.getValue();
} finally {
httpClient.getConnectionManager().shutdown();
}
}