當前位置: 首頁>>代碼示例>>Java>>正文


Java CloseableHttpResponse.getAllHeaders方法代碼示例

本文整理匯總了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();
}
 
開發者ID:melonlee,項目名稱:PowerApi,代碼行數:31,代碼來源:HttpUtil.java

示例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;
}
 
開發者ID:SoftGorilla,項目名稱:restheart-java-client,代碼行數:24,代碼來源:RestHeartClientApi.java

示例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;
}
 
開發者ID:intuit,項目名稱:karate,代碼行數:47,代碼來源:ApacheHttpClient.java

示例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;
}
 
開發者ID:EHRI,項目名稱:rs-aggregator,代碼行數:31,代碼來源:AbstractUriReader.java

示例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();
}
 
開發者ID:renatoathaydes,項目名稱:rawhttp,代碼行數:14,代碼來源:RawHttpComponentsClient.java

示例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);
    }
}
 
開發者ID:iterate-ch,項目名稱:cyberduck,代碼行數:52,代碼來源:HttpComponentsConnector.java


注:本文中的org.apache.http.client.methods.CloseableHttpResponse.getAllHeaders方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。