本文整理了Java中org.apache.hc.core5.http.HttpRequest.getHeaders()
方法的一些代码示例,展示了HttpRequest.getHeaders()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getHeaders()
方法的具体详情如下:
包路径:org.apache.hc.core5.http.HttpRequest
类名称:HttpRequest
方法名:getHeaders
暂无
代码示例来源:origin: apache/httpcomponents-client
private boolean hasValidDateField(final HttpRequest request, final String headerName) {
for(final Header h : request.getHeaders(headerName)) {
final Date date = DateUtils.parseDate(h.getValue());
return date != null;
}
return false;
}
}
代码示例来源:origin: apache/httpcomponents-client
buf.append(URLEncoder.encode(getFullHeaderValue(req.getHeaders(headerName)),
StandardCharsets.UTF_8.name()));
first = false;
代码示例来源:origin: apache/httpcomponents-client
/**
* When a {@link HttpCacheEntry} does not exist for a specific
* {@link org.apache.hc.core5.http.HttpRequest} we attempt to see if an existing
* {@link HttpCacheEntry} is appropriate by building a conditional
* {@link org.apache.hc.core5.http.HttpRequest} using the variants' ETag values.
* If no such values exist, the request is unmodified
*
* @param request the original request from the caller
* @param variants
* @return the wrapped request
*/
public T buildConditionalRequestFromVariants(final T request, final Map<String, Variant> variants) {
final T newRequest = messageCopier.copy(request);
newRequest.setHeaders(request.getHeaders());
// we do not support partial content so all etags are used
final StringBuilder etags = new StringBuilder();
boolean first = true;
for(final String etag : variants.keySet()) {
if (!first) {
etags.append(",");
}
first = false;
etags.append(etag);
}
newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
return newRequest;
}
代码示例来源:origin: apache/httpcomponents-client
/**
* Check entry against If-Modified-Since, if If-Modified-Since is in the future it is invalid as per
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
* @param request The current httpRequest being made
* @param entry the cache entry
* @param now right NOW in time
* @return boolean Does the last modified header match
*/
private boolean lastModifiedValidatorMatches(final HttpRequest request, final HttpCacheEntry entry, final Date now) {
final Date lastModified = DateUtils.parseDate(entry, HeaderConstants.LAST_MODIFIED);
if (lastModified == null) {
return false;
}
for (final Header h : request.getHeaders(HeaderConstants.IF_MODIFIED_SINCE)) {
final Date ifModifiedSince = DateUtils.parseDate(h.getValue());
if (ifModifiedSince != null) {
if (ifModifiedSince.after(now) || lastModified.after(ifModifiedSince)) {
return false;
}
}
}
return true;
}
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testGetVariantURIHandlesMultipleLineRequestHeaders() {
final String theURI = "theURI";
final Header[] varyHeaders = { new BasicHeader("Vary", "User-Agent, Accept-Encoding") };
final Header[] encHeaders = { new BasicHeader("Accept-Encoding", "gzip"),
new BasicHeader("Accept-Encoding", "deflate") };
final Header[] uaHeaders = { new BasicHeader("User-Agent", "browser") };
extractor = new CacheKeyGenerator() {
@Override
public String generateKey(final HttpHost h, final HttpRequest request) {
Assert.assertSame(defaultHost, h);
Assert.assertSame(mockRequest, request);
return theURI;
}
};
when(mockEntry.hasVariants()).thenReturn(true);
when(mockEntry.headerIterator("Vary")).thenReturn(new BasicHeaderIterator(varyHeaders, "Vary"));
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
final String result = extractor.generateKey(defaultHost, mockRequest, mockEntry);
verify(mockEntry).hasVariants();
verify(mockEntry).headerIterator("Vary");
verify(mockRequest).getHeaders("Accept-Encoding");
verify(mockRequest).getHeaders("User-Agent");
Assert.assertEquals("{Accept-Encoding=gzip%2C+deflate&User-Agent=browser}" + theURI, result);
}
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testGetVariantURIAlphabetizesWithMultipleVaryingHeaders() {
final String theURI = "theURI";
final Header[] varyHeaders = { new BasicHeader("Vary", "User-Agent, Accept-Encoding") };
final Header[] encHeaders = { new BasicHeader("Accept-Encoding", "gzip") };
final Header[] uaHeaders = { new BasicHeader("User-Agent", "browser") };
extractor = new CacheKeyGenerator() {
@Override
public String generateKey(final HttpHost h, final HttpRequest request) {
Assert.assertSame(defaultHost, h);
Assert.assertSame(mockRequest, request);
return theURI;
}
};
when(mockEntry.hasVariants()).thenReturn(true);
when(mockEntry.headerIterator("Vary")).thenReturn(new BasicHeaderIterator(varyHeaders, "Vary"));
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
final String result = extractor.generateKey(defaultHost, mockRequest, mockEntry);
verify(mockEntry).hasVariants();
verify(mockEntry).headerIterator("Vary");
verify(mockRequest).getHeaders("Accept-Encoding");
verify(mockRequest).getHeaders("User-Agent");
Assert.assertEquals("{Accept-Encoding=gzip&User-Agent=browser}" + theURI, result);
}
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testGetVariantURIHandlesMultipleVaryHeaders() {
final String theURI = "theURI";
final Header[] varyHeaders = { new BasicHeader("Vary", "User-Agent"),
new BasicHeader("Vary", "Accept-Encoding") };
final Header[] encHeaders = { new BasicHeader("Accept-Encoding", "gzip") };
final Header[] uaHeaders = { new BasicHeader("User-Agent", "browser") };
extractor = new CacheKeyGenerator() {
@Override
public String generateKey(final HttpHost h, final HttpRequest request) {
Assert.assertSame(defaultHost, h);
Assert.assertSame(mockRequest, request);
return theURI;
}
};
when(mockEntry.hasVariants()).thenReturn(true);
when(mockEntry.headerIterator("Vary")).thenReturn(new BasicHeaderIterator(varyHeaders, "Vary"));
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
when(mockRequest.getHeaders("User-Agent")).thenReturn(uaHeaders);
final String result = extractor.generateKey(defaultHost, mockRequest, mockEntry);
verify(mockEntry).hasVariants();
verify(mockEntry).headerIterator("Vary");
verify(mockRequest).getHeaders("Accept-Encoding");
verify(mockRequest).getHeaders("User-Agent");
Assert.assertEquals("{Accept-Encoding=gzip&User-Agent=browser}" + theURI, result);
}
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testGetVariantURIWithMissingRequestHeader() {
final String theURI = "theURI";
final Header[] noHeaders = new Header[0];
final Header[] varyHeaders = { new BasicHeader("Vary", "Accept-Encoding") };
extractor = new CacheKeyGenerator() {
@Override
public String generateKey(final HttpHost h, final HttpRequest request) {
Assert.assertSame(defaultHost, h);
Assert.assertSame(mockRequest, request);
return theURI;
}
};
when(mockEntry.hasVariants()).thenReturn(true);
when(mockEntry.headerIterator("Vary")).thenReturn(new BasicHeaderIterator(varyHeaders, "Vary"));
when(mockRequest.getHeaders("Accept-Encoding"))
.thenReturn(noHeaders);
final String result = extractor.generateKey(defaultHost, mockRequest, mockEntry);
verify(mockEntry).hasVariants();
verify(mockEntry).headerIterator("Vary");
verify(mockRequest).getHeaders("Accept-Encoding");
Assert.assertEquals("{Accept-Encoding=}" + theURI, result);
}
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testGetVariantURIWithSingleValueVaryHeaderPrepends() {
final String theURI = "theURI";
final Header[] varyHeaders = { new BasicHeader("Vary", "Accept-Encoding") };
final Header[] encHeaders = { new BasicHeader("Accept-Encoding", "gzip") };
extractor = new CacheKeyGenerator() {
@Override
public String generateKey(final HttpHost h, final HttpRequest request) {
Assert.assertSame(defaultHost, h);
Assert.assertSame(mockRequest, request);
return theURI;
}
};
when(mockEntry.hasVariants()).thenReturn(true);
when(mockEntry.headerIterator("Vary")).thenReturn(new BasicHeaderIterator(varyHeaders, "Vary"));
when(mockRequest.getHeaders("Accept-Encoding")).thenReturn(encHeaders);
final String result = extractor.generateKey(defaultHost, mockRequest, mockEntry);
verify(mockEntry).hasVariants();
verify(mockEntry).headerIterator("Vary");
verify(mockRequest).getHeaders("Accept-Encoding");
Assert.assertEquals("{Accept-Encoding=gzip}" + theURI, result);
}
代码示例来源:origin: apache/httpcomponents-client
newRequest.setHeaders(request.getHeaders());
final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
if (eTag != null) {
代码示例来源:origin: apache/httpcomponents-client
@Test
public void testBuildConditionalRequestWithETag() {
final String theMethod = "GET";
final String theUri = "/theuri";
final String theETag = "this is my eTag";
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", DateUtils.formatDate(new Date())),
new BasicHeader("ETag", theETag) };
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(3, newRequest.getHeaders().length);
Assert.assertEquals("Accept-Encoding", newRequest.getHeaders()[0].getName());
Assert.assertEquals("gzip", newRequest.getHeaders()[0].getValue());
Assert.assertEquals("If-None-Match", newRequest.getHeaders()[1].getName());
Assert.assertEquals(theETag, newRequest.getHeaders()[1].getValue());
}
代码示例来源: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.core5/httpcore5-testing
@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-cache
private boolean hasValidDateField(final HttpRequest request, final String headerName) {
for(final Header h : request.getHeaders(headerName)) {
final Date date = DateUtils.parseDate(h.getValue());
return date != null;
}
return false;
}
}
代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5-cache
buf.append(URLEncoder.encode(getFullHeaderValue(req.getHeaders(headerName)),
StandardCharsets.UTF_8.name()));
first = false;
代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5-cache
/**
* When a {@link HttpCacheEntry} does not exist for a specific
* {@link org.apache.hc.core5.http.HttpRequest} we attempt to see if an existing
* {@link HttpCacheEntry} is appropriate by building a conditional
* {@link org.apache.hc.core5.http.HttpRequest} using the variants' ETag values.
* If no such values exist, the request is unmodified
*
* @param request the original request from the caller
* @param variants
* @return the wrapped request
*/
public T buildConditionalRequestFromVariants(final T request, final Map<String, Variant> variants) {
final T newRequest = messageCopier.copy(request);
newRequest.setHeaders(request.getHeaders());
// we do not support partial content so all etags are used
final StringBuilder etags = new StringBuilder();
boolean first = true;
for(final String etag : variants.keySet()) {
if (!first) {
etags.append(",");
}
first = false;
etags.append(etag);
}
newRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
return newRequest;
}
代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5-cache
/**
* Check entry against If-Modified-Since, if If-Modified-Since is in the future it is invalid as per
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
* @param request The current httpRequest being made
* @param entry the cache entry
* @param now right NOW in time
* @return boolean Does the last modified header match
*/
private boolean lastModifiedValidatorMatches(final HttpRequest request, final HttpCacheEntry entry, final Date now) {
final Date lastModified = DateUtils.parseDate(entry, HeaderConstants.LAST_MODIFIED);
if (lastModified == null) {
return false;
}
for (final Header h : request.getHeaders(HeaderConstants.IF_MODIFIED_SINCE)) {
final Date ifModifiedSince = DateUtils.parseDate(h.getValue());
if (ifModifiedSince != null) {
if (ifModifiedSince.after(now) || lastModified.after(ifModifiedSince)) {
return false;
}
}
}
return true;
}
代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5-cache
newRequest.setHeaders(request.getHeaders());
final Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
if (eTag != null) {
代码示例来源:origin: org.apache.httpcomponents.client5/httpclient5
if (!StandardMethods.isSafe(request.getMethod())) {
final HttpRequest httpGet = new BasicHttpRequest(StandardMethods.GET.name(), redirectUri);
httpGet.setHeaders(scope.originalRequest.getHeaders());
state.currentRequest = httpGet;
state.currentEntityProducer = null;
内容来源于网络,如有侵权,请联系作者删除!