org.apache.http.Header.getValue()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(250)

本文整理了Java中org.apache.http.Header.getValue()方法的一些代码示例,展示了Header.getValue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Header.getValue()方法的具体详情如下:
包路径:org.apache.http.Header
类名称:Header
方法名:getValue

Header.getValue介绍

[英]Get the value of the Header.
[中]获取标题的值。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Determine whether the given response indicates a GZIP response.
 * <p>The default implementation checks whether the HTTP "Content-Encoding"
 * header contains "gzip" (in any casing).
 * @param httpResponse the resulting HttpResponse to check
 * @return whether the given response indicates a GZIP response
 */
protected boolean isGzipResponse(HttpResponse httpResponse) {
  Header encodingHeader = httpResponse.getFirstHeader(HTTP_HEADER_CONTENT_ENCODING);
  return (encodingHeader != null && encodingHeader.getValue() != null &&
      encodingHeader.getValue().toLowerCase().contains(ENCODING_GZIP));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public HttpHeaders getHeaders() {
  if (this.headers == null) {
    this.headers = new HttpHeaders();
    for (Header header : this.httpResponse.getAllHeaders()) {
      this.headers.add(header.getName(), header.getValue());
    }
  }
  return this.headers;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public String getContentEncoding() {
  return (response == null || response.getEntity() == null || response.getEntity().getContentEncoding() == null) ? null : response.getEntity().getContentEncoding().getValue();
}

代码示例来源:origin: wiztools/rest-client

static String getHTTPResponseTrace(HttpResponse response) {
  StringBuilder sb = new StringBuilder();
  sb.append(response.getStatusLine()).append('\n');
  for (Header h : response.getAllHeaders()) {
    sb.append(h.getName()).append(": ").append(h.getValue()).append('\n');
  }
  sb.append('\n');
  HttpEntity e = response.getEntity();
  if (e != null) {
    appendHttpEntity(sb, e);
  }
  return sb.toString();
}

代码示例来源:origin: Kong/unirest-java

@SuppressWarnings("unchecked")
public HttpResponse(org.apache.http.HttpResponse response, Class<T> responseClass) {
  HttpEntity responseEntity = response.getEntity();
  ObjectMapper objectMapper = (ObjectMapper) Options.getOption(Option.OBJECT_MAPPER);
    String headerName = header.getName();
    List<String> list = headers.get(headerName);
    if (list == null)
      list = new ArrayList<String>();
    list.add(header.getValue());
    headers.put(headerName, list);
  StatusLine statusLine = response.getStatusLine();
  this.statusCode = statusLine.getStatusCode();
  this.statusText = statusLine.getReasonPhrase();
      String responseCharset = ResponseUtils.getCharsetFromContentType(contentType.getValue());
      if (responseCharset != null && !responseCharset.trim().equals("")) {
        charset = responseCharset;
      byte[] rawBody;
      try {
        InputStream responseInputStream = responseEntity.getContent();
        if (ResponseUtils.isGzipped(responseEntity.getContentEncoding())) {
          responseInputStream = new GZIPInputStream(responseEntity.getContent());

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldForwardHttpsAndHost() throws Exception
{
  URI rootUri = functionalTestHelper.baseUri();
  HttpClient httpclient = new DefaultHttpClient();
  try
  {
    HttpGet httpget = new HttpGet( rootUri );
    httpget.setHeader( "Accept", "application/json" );
    httpget.setHeader( "X-Forwarded-Host", "foobar.com" );
    httpget.setHeader( "X-Forwarded-Proto", "https" );
    HttpResponse response = httpclient.execute( httpget );
    String length = response.getHeaders( "CONTENT-LENGTH" )[0].getValue();
    byte[] data = new byte[Integer.valueOf( length )];
    response.getEntity().getContent().read( data );
    String responseEntityBody = new String( data );
    assertTrue( responseEntityBody.contains( "https://foobar.com" ) );
    assertFalse( responseEntityBody.contains( "https://localhost" ) );
  }
  finally
  {
    httpclient.getConnectionManager().shutdown();
  }
}

代码示例来源:origin: sarxos/webcam-capture

private InputStream get(final URI uri, boolean withoutImageMime) throws UnsupportedOperationException, IOException {
  final HttpGet get = new HttpGet(uri);
  final HttpResponse respone = client.execute(get, context);
  final HttpEntity entity = respone.getEntity();
  // normal jpeg return image/jpeg as opposite to mjpeg
  if (withoutImageMime) {
    final Header contentType = entity.getContentType();
    if (contentType == null) {
      throw new WebcamException("Content Type header is missing");
    }
    if (contentType.getValue().startsWith("image/")) {
      throw new WebcamException("Cannot read images in PUSH mode, change mode to PULL " + contentType);
    }
  }
  return entity.getContent();
}

代码示例来源:origin: alibaba/canal

private static void saveFile(File parentFile, String fileName, HttpResponse response) throws IOException {
  InputStream is = response.getEntity().getContent();
  long totalSize = Long.parseLong(response.getFirstHeader("Content-Length").getValue());
  if (response.getFirstHeader("Content-Disposition") != null) {
    fileName = response.getFirstHeader("Content-Disposition").getValue();
    fileName = StringUtils.substringAfter(fileName, "filename=");

代码示例来源:origin: aws/aws-sdk-java

int statusCode = response.getStatusLine().getStatusCode();
  throw new AmazonClientException("Could not fetch activation key, no location header found");
String activationUrl = headers[0].getValue();
String[] parts = activationUrl.split("activationKey=");

代码示例来源:origin: medcl/elasticsearch-analysis-ik

try {
  response = httpclient.execute(get);
  if (response.getStatusLine().getStatusCode() == 200) {
    if (response.getEntity().getContentType().getValue().contains("charset=")) {
      String contentType = response.getEntity().getContentType().getValue();
      charset = contentType.substring(contentType.lastIndexOf("=") + 1);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
    String line;
    while ((line = in.readLine()) != null) {

代码示例来源:origin: dropwizard/dropwizard

/**
 * {@inheritDoc}
 */
@Override
public ClientResponse apply(ClientRequest jerseyRequest) {
  try {
    final HttpUriRequest apacheRequest = buildApacheRequest(jerseyRequest);
    final CloseableHttpResponse apacheResponse = client.execute(apacheRequest);
    final StatusLine statusLine = apacheResponse.getStatusLine();
    final String reasonPhrase = Strings.nullToEmpty(statusLine.getReasonPhrase());
    final Response.StatusType status = Statuses.from(statusLine.getStatusCode(), reasonPhrase);
    final ClientResponse jerseyResponse = new ClientResponse(status, jerseyRequest);
    for (Header header : apacheResponse.getAllHeaders()) {
      jerseyResponse.getHeaders().computeIfAbsent(header.getName(), k -> new ArrayList<>())
        .add(header.getValue());
    }
    final HttpEntity httpEntity = apacheResponse.getEntity();
    jerseyResponse.setEntityStream(httpEntity != null ? httpEntity.getContent() :
        new ByteArrayInputStream(new byte[0]));
    return jerseyResponse;
  } catch (Exception e) {
    throw new ProcessingException(e);
  }
}

代码示例来源:origin: apache/incubator-pinot

private static String getErrorMessage(HttpUriRequest request, CloseableHttpResponse response) {
 String controllerHost = null;
 String controllerVersion = null;
 if (response.containsHeader(CommonConstants.Controller.HOST_HTTP_HEADER)) {
  controllerHost = response.getFirstHeader(CommonConstants.Controller.HOST_HTTP_HEADER).getValue();
  controllerVersion = response.getFirstHeader(CommonConstants.Controller.VERSION_HTTP_HEADER).getValue();
 }
 StatusLine statusLine = response.getStatusLine();
 String reason;
 try {
  reason = JsonUtils.stringToJsonNode(EntityUtils.toString(response.getEntity())).get("error").asText();
 } catch (Exception e) {
  reason = "Failed to get reason";
 }
 String errorMessage = String.format("Got error status code: %d (%s) with reason: \"%s\" while sending request: %s",
   statusLine.getStatusCode(), statusLine.getReasonPhrase(), reason, request.getURI());
 if (controllerHost != null) {
  errorMessage =
    String.format("%s to controller: %s, version: %s", errorMessage, controllerHost, controllerVersion);
 }
 return errorMessage;
}

代码示例来源:origin: alibaba/Sentinel

private String getBody(HttpResponse response) throws Exception {
  Charset charset = null;
  try {
    String contentTypeStr = response.getFirstHeader("Content-type").getValue();
    if (StringUtil.isNotEmpty(contentTypeStr)) {
      ContentType contentType = ContentType.parse(contentTypeStr);
      charset = contentType.getCharset();
    }
  } catch (Exception ignore) {
  }
  return EntityUtils.toString(response.getEntity(), charset != null ? charset : DEFAULT_CHARSET);
}

代码示例来源:origin: apache/geode

public HttpResponseAssert hasContentType(String contentType) {
 assertThat(actual.getEntity().getContentType().getValue()).containsIgnoringCase(contentType);
 return this;
}

代码示例来源:origin: square/okhttp

String name = header.getName();
if ("Content-Type".equalsIgnoreCase(name)) {
 contentType = header.getValue();
} else {
 builder.header(name, header.getValue());
  builder.header(encoding.getName(), encoding.getValue());

代码示例来源:origin: rapidoid/rapidoid

private static HttpResp response(HttpResponse response) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  PrintWriter printer = new PrintWriter(baos);
  printer.print(response.getStatusLine() + "");
  printer.print("\n");
  Map<String, String> headers = U.map();
  for (Header hdr : response.getAllHeaders()) {
    printer.print(hdr.getName());
    printer.print(": ");
    printer.print(hdr.getValue());
    printer.print("\n");
    headers.put(hdr.getName(), hdr.getValue());
  }
  printer.print("\n");
  printer.flush();
  HttpEntity entity = response.getEntity();
  byte[] body = entity != null ? IO.loadBytes(response.getEntity().getContent()) : new byte[0];
  baos.write(body);
  byte[] raw = baos.toByteArray();
  return new HttpResp(raw, response.getStatusLine().getStatusCode(), headers, body);
}

代码示例来源:origin: neo4j/neo4j

@Test
public void shouldForwardHttpAndHost() throws Exception
{
  URI rootUri = functionalTestHelper.baseUri();
  HttpClient httpclient = new DefaultHttpClient();
  try
  {
    HttpGet httpget = new HttpGet( rootUri );
    httpget.setHeader( "Accept", "application/json" );
    httpget.setHeader( "X-Forwarded-Host", "foobar.com" );
    httpget.setHeader( "X-Forwarded-Proto", "http" );
    HttpResponse response = httpclient.execute( httpget );
    String length = response.getHeaders( "CONTENT-LENGTH" )[0].getValue();
    byte[] data = new byte[Integer.valueOf( length )];
    response.getEntity().getContent().read( data );
    String responseEntityBody = new String( data );
    assertTrue( responseEntityBody.contains( "http://foobar.com" ) );
    assertFalse( responseEntityBody.contains( "http://localhost" ) );
  }
  finally
  {
    httpclient.getConnectionManager().shutdown();
  }
}

代码示例来源:origin: mitreid-connect/OpenID-Connect-Java-Spring-Server

@Override
public CachedImage load(ClientDetailsEntity key) throws Exception {
  try {
    HttpResponse response = httpClient.execute(new HttpGet(key.getLogoUri()));
    HttpEntity entity = response.getEntity();
    CachedImage image = new CachedImage();
    image.setContentType(entity.getContentType().getValue());
    image.setLength(entity.getContentLength());
    image.setData(IOUtils.toByteArray(entity.getContent()));
    return image;
  } catch (IOException e) {
    throw new IllegalArgumentException("Unable to load client image.");
  }
}

代码示例来源:origin: dreamhead/moco

@Override
  public void run() throws IOException {
    org.apache.http.HttpResponse httpResponse = helper.getResponse(remoteUrl("/dir/dir.response"));
    String value = httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue();
    assertThat(value, is("text/plain"));
    String content = CharStreams.toString(new InputStreamReader(httpResponse.getEntity().getContent()));
    assertThat(content, is("response from dir"));
  }
});

代码示例来源:origin: dreamhead/moco

@Override
  public void run() throws Exception {
    HttpResponse httpResponse = helper.headForResponse(remoteUrl("/targets"));
    assertThat(httpResponse.getStatusLine().getStatusCode(), is(200));
    assertThat(httpResponse.getHeaders("ETag")[0].getValue(), is("Moco"));
  }
});

相关文章