org.apache.hc.core5.http.HttpRequest类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(1027)

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

HttpRequest介绍

[英]A request message from a client to a server includes, within the first line of that message, the method to be applied to the resource, the identifier of the resource, and the protocol version in use.
[中]从客户机到服务器的请求消息在该消息的第一行中包括要应用于资源的方法、资源的标识符和正在使用的协议版本。

代码示例

代码示例来源:origin: apache/httpcomponents-client

private void decrementOPTIONSMaxForwardsIfGreaterThen0(final HttpRequest request) {
  if (!HeaderConstants.OPTIONS_METHOD.equals(request.getMethod())) {
    return;
  }
  final Header maxForwards = request.getFirstHeader(HeaderConstants.MAX_FORWARDS);
  if (maxForwards == null) {
    return;
  }
  request.removeHeaders(HeaderConstants.MAX_FORWARDS);
  final int currentMaxForwards = Integer.parseInt(maxForwards.getValue());
  request.setHeader(HeaderConstants.MAX_FORWARDS, Integer.toString(currentMaxForwards - 1));
}

代码示例来源:origin: apache/httpcomponents-client

Args.notNull(target, "Target");
final StringBuilder buf = new StringBuilder();
final URIAuthority authority = request.getAuthority();
if (authority != null) {
  final String scheme = request.getScheme();
  buf.append(scheme != null ? scheme : URIScheme.HTTP.id).append("://");
  buf.append(authority.getHostName());
final String path = request.getPath();
if (path == null) {
  buf.append("/");

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildUnconditionalRequestDoesNotUseIfMatch()
  throws Exception {
  request.addHeader("If-Match","\"etag\"");
  final HttpRequest result = impl.buildUnconditionalRequest(request);
  Assert.assertNull(result.getFirstHeader("If-Match"));
}

代码示例来源:origin: apache/httpcomponents-client

boolean clientRequestsOurOptions(final HttpRequest request) {
  if (!HeaderConstants.OPTIONS_METHOD.equals(request.getMethod())) {
    return false;
  }
  if (!"*".equals(request.getRequestUri())) {
    return false;
  }
  final Header h = request.getFirstHeader(HeaderConstants.MAX_FORWARDS);
  if (!"0".equals(h != null ? h.getValue() : null)) {
    return false;
  }
  return true;
}

代码示例来源:origin: apache/httpcomponents-client

private RequestProtocolError requestHasWeakETagAndRange(final HttpRequest request) {
  // TODO: Should these be looking at all the headers marked as Range?
  final String method = request.getMethod();
  if (!(HeaderConstants.GET_METHOD.equals(method))) {
    return null;
  }
  final Header range = request.getFirstHeader(HeaderConstants.RANGE);
  if (range == null) {
    return null;
  }
  final Header ifRange = request.getFirstHeader(HeaderConstants.IF_RANGE);
  if (ifRange == null) {
    return null;
  }
  final String val = ifRange.getValue();
  if (val.startsWith("W/")) {
    return RequestProtocolError.WEAK_ETAG_AND_RANGE_ERROR;
  }
  return null;
}

代码示例来源:origin: apache/httpcomponents-core

@Test
public void testRequestWithRelativeURI() throws Exception {
  final HttpRequest request = new BasicHttpRequest("GET", new URI("/stuff"));
  Assert.assertEquals("GET", request.getMethod());
  Assert.assertEquals("/stuff", request.getPath());
  Assert.assertEquals(null, request.getAuthority());
  Assert.assertEquals(new URI("/stuff"), request.getUri());
}

代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5

public static SimpleHttpRequest copy(final HttpRequest original) {
  Args.notNull(original, "HTTP request");
  final SimpleHttpRequest copy = new SimpleHttpRequest(original.getMethod(), original.getRequestUri());
  copy.setVersion(original.getVersion());
  for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
    copy.addHeader(it.next());
  }
  copy.setScheme(original.getScheme());
  copy.setAuthority(original.getAuthority());
  return copy;
}

代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5

@Override
public HttpRequest copy(final HttpRequest original) {
  if (original == null) {
    return null;
  }
  final BasicHttpRequest copy = new BasicHttpRequest(original.getMethod(), original.getPath());
  copy.setVersion(original.getVersion());
  for (final Iterator<Header> it = original.headerIterator(); it.hasNext(); ) {
    copy.addHeader(it.next());
  }
  copy.setScheme(original.getScheme());
  copy.setAuthority(original.getAuthority());
  return copy;
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testBuildConditionalRequestWithLastModified() {
  final String theMethod = "GET";
  final String theUri = "/theuri";
  final String lastModified = "this is my last modified date";
  final HttpRequest basicRequest = new BasicHttpRequest(theMethod, theUri);
  basicRequest.addHeader("Accept-Encoding", "gzip");
  final HttpRequest requestWrapper = RequestCopier.INSTANCE.copy(basicRequest);
  final Header[] headers = new Header[] {
      new BasicHeader("Date", DateUtils.formatDate(new Date())),
      new BasicHeader("Last-Modified", lastModified) };
  final HttpCacheEntry cacheEntry = HttpTestUtils.makeCacheEntry(headers);
  final HttpRequest newRequest = impl.buildConditionalRequest(requestWrapper, cacheEntry);
  Assert.assertNotSame(basicRequest, newRequest);
  Assert.assertEquals(theMethod, newRequest.getMethod());
  Assert.assertEquals(theUri, newRequest.getRequestUri());
  Assert.assertEquals(2, newRequest.getHeaders().length);
  Assert.assertEquals("Accept-Encoding", newRequest.getHeaders()[0].getName());
  Assert.assertEquals("gzip", newRequest.getHeaders()[0].getValue());
  Assert.assertEquals("If-Modified-Since", newRequest.getHeaders()[1].getName());
  Assert.assertEquals(lastModified, newRequest.getHeaders()[1].getValue());
}

代码示例来源:origin: apache/httpcomponents-core

@Override
public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
  if (config.getVerbosity() >= 3) {
    System.out.println(">> " + request.getMethod() + " " + request.getRequestUri());
    final Header[] headers = request.getHeaders();
    for (final Header header : headers) {
      System.out.println(">> " + header.toString());
    }
    System.out.println();
  }
}

代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5

URI uri = request.getUri();
    if (!uri.isAbsolute()) {
      uri = URIUtils.rewriteURI(uri, route.getTargetHost(), true);
      uri = URIUtils.rewriteURI(uri);
    request.setPath(uri.toASCIIString());
  } catch (final URISyntaxException ex) {
    throw new ProtocolException("Invalid request URI: " + request.getRequestUri(), ex);
final URIAuthority authority = request.getAuthority();
if (authority != null) {
  final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
  if (credsProvider instanceof CredentialsStore) {
    AuthSupport.extractFromAuthority(request.getScheme(), authority, (CredentialsStore) credsProvider);

代码示例来源:origin: apache/httpcomponents-client

private boolean isGet(final HttpRequest request) {
  return request.getMethod().equals(HeaderConstants.GET_METHOD);
}

代码示例来源:origin: apache/httpcomponents-client

@Test
public void stripsMaxAgeFromRequestIfNoCachePresent()
  throws Exception {
  final HttpRequest req = new BasicHttpRequest("GET", "/");
  req.setHeader("Cache-Control", "no-cache, max-age=10");
  final HttpRequest wrapper = RequestCopier.INSTANCE.copy(req);
  impl.makeRequestCompliant(wrapper);
  assertEquals("no-cache",
      wrapper.getFirstHeader("Cache-Control").getValue());
}

代码示例来源:origin: apache/httpcomponents-client

public static boolean equivalent(final HttpRequest r1, final HttpRequest r2) {
  return equivalent(r1.getVersion(), r2.getVersion()) &&
      LangUtils.equals(r1.getMethod(), r2.getMethod()) &&
      LangUtils.equals(r1.getRequestUri(), r2.getRequestUri()) &&
      isEndToEndHeaderSubset(r1, r2);
}

代码示例来源:origin: apache/httpcomponents-client

newRequest.setHeaders(request.getHeaders());
final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
if (eTag != null) {
  newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
  newRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue());
  newRequest.addHeader(HeaderConstants.CACHE_CONTROL, HeaderConstants.CACHE_CONTROL_MAX_AGE + "=0");

代码示例来源:origin: apache/httpcomponents-client

@Override
public void handleRequest(
    final HttpRequest request,
    final EntityDetails entityDetails,
    final ResponseChannel responseChannel,
    final HttpContext context) throws HttpException, IOException {
  final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
  final String challengeResponse = h != null ? authTokenExtractor.extract(h.getValue()) : null;
  final URIAuthority authority = request.getAuthority();
  final String requestUri = request.getRequestUri();
  final boolean authenticated = authenticator.authenticate(authority, requestUri, challengeResponse);
  final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
  final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
  if (authenticated) {
    if (expectContinue) {
      responseChannel.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE), context);
    }
    exchangeHandler.handleRequest(request, entityDetails, responseChannel, context);
  } else {
    final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
    final String realm = authenticator.getRealm(authority, requestUri);
    unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Basic realm=\"" + realm + "\"");
    customizeUnauthorizedResponse(unauthorized);
    final AsyncResponseProducer responseProducer = new BasicResponseProducer(
        unauthorized,
        new BasicAsyncEntityProducer("Unauthorized", ContentType.TEXT_PLAIN));
    responseProducerRef.set(responseProducer);
    responseProducer.sendResponse(responseChannel, context);
  }
}

代码示例来源:origin: apache/httpcomponents-client

context.setAttribute(HttpClientContext.HTTP_REQUEST, request);
final URIAuthority authority = request.getAuthority();
final String scheme = request.getScheme();
final HttpHost target = authority != null ? new HttpHost(scheme, authority) : route.getTargetHost();
final String via = generateViaHeader(request);
request.addHeader("Via",via);

代码示例来源:origin: apache/httpcomponents-core

httpRequest.setVersion(HttpVersion.HTTP_2);
httpRequest.setScheme(scheme);
try {
  httpRequest.setAuthority(URIAuthority.create(authority));
} catch (final URISyntaxException ex) {
  throw new ProtocolException(ex.getMessage(), ex);
httpRequest.setPath(path);
for (int i = 0; i < messageHeaders.size(); i++) {
  httpRequest.addHeader(messageHeaders.get(i));

代码示例来源:origin: apache/httpcomponents-client

@Test
public void testSuitableIfFreshAndFreshnessLifetimeGreaterThanRequestMinFresh() {
  request.addHeader("Cache-Control", "min-fresh=10");
  final Header[] headers = {
      new BasicHeader("Date", DateUtils.formatDate(tenSecondsAgo)),
      new BasicHeader("Cache-Control", "max-age=3600"),
      new BasicHeader("Content-Length","128")
  };
  entry = getEntry(headers);
  Assert.assertTrue(impl.canCachedResponseBeUsed(host, request, entry, now));
}

代码示例来源:origin: apache/httpcomponents-client

void recordCacheMiss(final HttpHost target, final HttpRequest request) {
  cacheMisses.getAndIncrement();
  if (log.isTraceEnabled()) {
    log.debug("Cache miss [host: " + target + "; uri: " + request.getRequestUri() + "]");
  }
}

相关文章