下面列出了org.apache.http.client.methods.HttpHead#setHeader() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@Test
public void shouldPrintMultipleCookiesInOneParameter() throws Exception {
HttpHead headRequest = new HttpHead("http://test.com/items/12345");
headRequest.setHeader("Cookie", "X=Y; A=B");
assertThat(getNonWindowsHttp2Curl().generateCurl(headRequest),
equalTo("curl 'http://test.com/items/12345' -X HEAD -b 'X=Y; A=B' --compressed -k -v"));
}
/**
* Check if POM file for provided gav can be found in target. Just does
* a HTTP get of the header and verifies http status OK 200.
* @param targetUrl url of the target repository
* @param gav group artifact version string
* @return {@code true} if the pom.xml already exists in the target repository
*/
private boolean checkIfPomInTarget( String targetUrl, String username, String password, Gav gav )
{
boolean alreadyInTarget = false;
String artifactUrl = targetUrl + gav.getRepositoryURLPath() + gav.getPomFilename();
logger.debug( "Headers for {}", artifactUrl );
HttpHead httphead = new HttpHead( artifactUrl );
if ( !StringUtils.isEmpty( username ) && ! StringUtils.isEmpty( password ) )
{
String encoding = java.util.Base64.getEncoder().encodeToString( ( username + ":" + password ).getBytes() );
httphead.setHeader( "Authorization", "Basic " + encoding );
}
try ( CloseableHttpClient httpClient = HttpClientBuilder.create().build() )
{
HttpResponse response = httpClient.execute( httphead );
int statusCode = response.getStatusLine().getStatusCode();
if ( statusCode == HttpURLConnection.HTTP_OK )
{
alreadyInTarget = true;
}
else
{
logger.debug( "Headers not found HTTP: {}", statusCode );
}
}
catch ( IOException ioe )
{
logger.warn( "Could not check target repository for already existing pom.xml.", ioe );
}
return alreadyInTarget;
}
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();
}
}
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();
}
}