本文整理汇总了Java中org.apache.http.client.methods.CloseableHttpResponse.getAllHeaders方法的典型用法代码示例。如果您正苦于以下问题:Java CloseableHttpResponse.getAllHeaders方法的具体用法?Java CloseableHttpResponse.getAllHeaders怎么用?Java CloseableHttpResponse.getAllHeaders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.client.methods.CloseableHttpResponse
的用法示例。
在下文中一共展示了CloseableHttpResponse.getAllHeaders方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doResponse
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
/**
* 处理返回结果数据
*
* @param unitTest
* @param response
* @throws IOException
*/
private static void doResponse(UnitTest unitTest, CloseableHttpResponse response) throws IOException {
int statusCode = response.getStatusLine().getStatusCode();
unitTest.setResponseCode(statusCode);
StringBuffer sb = new StringBuffer();
for (int loop = 0; loop < response.getAllHeaders().length; loop++) {
BufferedHeader header = (BufferedHeader) response
.getAllHeaders()[loop];
if (header.getName().equals("Accept-Charset")) {
continue;
}
sb.append(header.getName() + ":" + header.getValue() + "<br/>");
}
unitTest.setResponseHeader(sb.toString());
HttpEntity entity = response.getEntity();
String result;
if (entity != null) {
result = EntityUtils.toString(entity, "utf-8");
unitTest.setResponseSize(result.getBytes().length);
unitTest.setResponseBody(result);
}
EntityUtils.consume(entity);
response.close();
}
示例2: extractFromResponse
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
private RestHeartClientResponse extractFromResponse(final CloseableHttpResponse httpResponse) {
RestHeartClientResponse response = null;
JsonObject responseObj = null;
if (httpResponse != null) {
StatusLine statusLine = httpResponse.getStatusLine();
Header[] allHeaders = httpResponse.getAllHeaders();
HttpEntity resEntity = httpResponse.getEntity();
if (resEntity != null) {
try {
String responseStr = IOUtils.toString(resEntity.getContent(), "UTF-8");
if (responseStr != null && !responseStr.isEmpty()) {
JsonParser parser = new JsonParser();
responseObj = parser.parse(responseStr).getAsJsonObject();
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Was unable to extract response body", e);
}
}
response = new RestHeartClientResponse(statusLine, allHeaders, responseObj);
}
return response;
}
示例3: makeHttpRequest
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse makeHttpRequest(HttpEntity entity, long startTime) {
if (entity != null) {
requestBuilder.setEntity(entity);
requestBuilder.setHeader(entity.getContentType());
}
HttpUriRequest httpRequest = requestBuilder.build();
CloseableHttpClient client = clientBuilder.build();
BasicHttpContext context = new BasicHttpContext();
context.setAttribute(URI_CONTEXT_KEY, getRequestUri());
CloseableHttpResponse httpResponse;
byte[] bytes;
try {
httpResponse = client.execute(httpRequest, context);
HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity == null || responseEntity.getContent() == null) {
bytes = new byte[0];
} else {
InputStream is = responseEntity.getContent();
bytes = FileUtils.toBytes(is);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
long responseTime = getResponseTime(startTime);
HttpResponse response = new HttpResponse(responseTime);
response.setUri(getRequestUri());
response.setBody(bytes);
response.setStatus(httpResponse.getStatusLine().getStatusCode());
for (Cookie c : cookieStore.getCookies()) {
com.intuit.karate.http.Cookie cookie = new com.intuit.karate.http.Cookie(c.getName(), c.getValue());
cookie.put(DOMAIN, c.getDomain());
cookie.put(PATH, c.getPath());
if (c.getExpiryDate() != null) {
cookie.put(EXPIRES, c.getExpiryDate().getTime() + "");
}
cookie.put(PERSISTENT, c.isPersistent() + "");
cookie.put(SECURE, c.isSecure() + "");
response.addCookie(cookie);
}
cookieStore.clear(); // we rely on the StepDefs for cookie 'persistence'
for (Header header : httpResponse.getAllHeaders()) {
response.addHeader(header.getName(), header.getValue());
}
return response;
}
示例4: execute
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
public <R> Result<R> execute(URI uri, LambdaUtil.BiFunction_WithExceptions<URI, HttpResponse, R, ?> func) {
logger.debug("Executing GET on uri {}", uri);
currentUri = uri;
Result<R> result = new Result<R>(uri);
HttpGet request = new HttpGet(uri);
CloseableHttpResponse response = null;
try {
response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
result.setStatusLine(response.getStatusLine().toString());
logger.debug("Received {} from {}", response.getStatusLine(), uri);
result.setStatusCode(statusCode);
if (keepingHeaders) {
for (Header header : response.getAllHeaders()) {
result.getHeaders().put(header.getName(), header.getValue());
}
}
if (statusCode < 200 || statusCode > 299) {
result.addError(new RemoteException(statusCode, response.getStatusLine().getReasonPhrase(), uri));
} else {
result.accept(func.apply(uri, response));
}
} catch (Exception e) {
logger.error("Error executing GET on uri {}", uri, e);
result.addError(e);
} finally {
closeResponse(response);
}
return result;
}
示例5: readHeaders
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
private RawHttpHeaders readHeaders(CloseableHttpResponse response) {
Header[] allHeaders = response.getAllHeaders();
RawHttpHeaders.Builder headers = RawHttpHeaders.Builder.newBuilder();
for (Header header : allHeaders) {
String meta = header.getElements().length > 0 ?
";" + Arrays.stream(header.getElements())
.flatMap(it -> Arrays.stream(it.getParameters()).map(v -> v.getName() + "=" + v.getValue()))
.collect(joining(";")) :
"";
headers.with(header.getName(), header.getValue() + meta);
}
return headers.build();
}
示例6: apply
import org.apache.http.client.methods.CloseableHttpResponse; //导入方法依赖的package包/类
@Override
public ClientResponse apply(final ClientRequest clientRequest) throws ProcessingException {
final HttpUriRequest request = this.toUriHttpRequest(clientRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(clientRequest.getHeaders(), request);
try {
final CloseableHttpResponse response;
response = client.execute(new HttpHost(request.getURI().getHost(), request.getURI().getPort(), request.getURI().getScheme()), request, new BasicHttpContext(context));
HeaderUtils.checkHeaderChanges(clientHeadersSnapshot, clientRequest.getHeaders(), this.getClass().getName());
final Response.StatusType status = response.getStatusLine().getReasonPhrase() == null
? Statuses.from(response.getStatusLine().getStatusCode())
: Statuses.from(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
final ClientResponse responseContext = new ClientResponse(status, clientRequest);
final List<URI> redirectLocations = context.getRedirectLocations();
if(redirectLocations != null && !redirectLocations.isEmpty()) {
responseContext.setResolvedRequestUri(redirectLocations.get(redirectLocations.size() - 1));
}
final Header[] respHeaders = response.getAllHeaders();
final MultivaluedMap<String, String> headers = responseContext.getHeaders();
for(final Header header : respHeaders) {
final String headerName = header.getName();
List<String> list = headers.get(headerName);
if(list == null) {
list = new ArrayList<>();
}
list.add(header.getValue());
headers.put(headerName, list);
}
final HttpEntity entity = response.getEntity();
if(entity != null) {
if(headers.get(HttpHeaders.CONTENT_LENGTH) == null) {
headers.add(HttpHeaders.CONTENT_LENGTH, String.valueOf(entity.getContentLength()));
}
final Header contentEncoding = entity.getContentEncoding();
if(headers.get(HttpHeaders.CONTENT_ENCODING) == null && contentEncoding != null) {
headers.add(HttpHeaders.CONTENT_ENCODING, contentEncoding.getValue());
}
}
responseContext.setEntityStream(this.toInputStream(response));
return responseContext;
}
catch(final Exception e) {
throw new ProcessingException(e);
}
}