本文整理了Java中org.springframework.http.HttpRequest.getHeaders()
方法的一些代码示例,展示了HttpRequest.getHeaders()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getHeaders()
方法的具体详情如下:
包路径:org.springframework.http.HttpRequest
类名称:HttpRequest
方法名:getHeaders
暂无
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the headers of the wrapped request.
*/
@Override
public HttpHeaders getHeaders() {
return this.request.getHeaders();
}
代码示例来源:origin: org.springframework/spring-web
/**
* Return the headers of the wrapped request.
*/
@Override
public HttpHeaders getHeaders() {
return this.request.getHeaders();
}
代码示例来源:origin: stagemonitor/stagemonitor
@Override
public void put(String key, String value) {
httpRequest.getHeaders().add(key, value);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
String token = Base64Utils.encodeToString(
(this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));
request.getHeaders().add("Authorization", "Basic " + token);
return execution.execute(request, body);
}
代码示例来源:origin: eugenp/tutorials
private void logRequest(HttpRequest request, byte[] body) throws UnsupportedEncodingException {
log.info("URI: " + request.getURI());
log.info("HTTP Method: " + request.getMethod());
log.info("HTTP Headers: " + headersToString(request.getHeaders()));
log.info("Request Body: " + new String(body, StandardCharsets.UTF_8));
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Check the given request origin against a list of allowed origins.
* A list containing "*" means that all origins are allowed.
* An empty list means only same origin is allowed.
*
* <p><strong>Note:</strong> as of 5.1 this method ignores
* {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
* client-originated address. Consider using the {@code ForwardedHeaderFilter}
* to extract and use, or to discard such headers.
*
* @return {@code true} if the request origin is valid, {@code false} otherwise
* @since 4.1.5
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
*/
public static boolean isValidOrigin(HttpRequest request, Collection<String> allowedOrigins) {
Assert.notNull(request, "Request must not be null");
Assert.notNull(allowedOrigins, "Allowed origins must not be null");
String origin = request.getHeaders().getOrigin();
if (origin == null || allowedOrigins.contains("*")) {
return true;
}
else if (CollectionUtils.isEmpty(allowedOrigins)) {
return isSameOrigin(request);
}
else {
return allowedOrigins.contains(origin);
}
}
代码示例来源:origin: org.springframework/spring-web
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
String token = Base64Utils.encodeToString(
(this.username + ":" + this.password).getBytes(StandardCharsets.UTF_8));
request.getHeaders().add("Authorization", "Basic " + token);
return execution.execute(request, body);
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
headers.setBasicAuth(this.username, this.password, this.charset);
}
return execution.execute(request, body);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new {@code UriComponents} object from the URI associated with
* the given HttpRequest while also overlaying with values from the headers
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
* "Forwarded" is not found.
* @param request the source request
* @return the URI components of the URI
* @since 4.1.5
*/
public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {
return fromUri(request.getURI()).adaptFromForwardedHeaders(request.getHeaders());
}
代码示例来源:origin: spring-projects/spring-framework
HttpHeaders headers = request.getHeaders();
String origin = headers.getOrigin();
if (origin == null) {
代码示例来源:origin: org.springframework/spring-web
@Override
public ClientHttpResponse intercept(
HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
HttpHeaders headers = request.getHeaders();
if (!headers.containsKey(HttpHeaders.AUTHORIZATION)) {
headers.setBasicAuth(this.username, this.password, this.charset);
}
return execution.execute(request, body);
}
代码示例来源:origin: org.springframework/spring-web
/**
* Create a new {@code UriComponents} object from the URI associated with
* the given HttpRequest while also overlaying with values from the headers
* "Forwarded" (<a href="http://tools.ietf.org/html/rfc7239">RFC 7239</a>),
* or "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" if
* "Forwarded" is not found.
* @param request the source request
* @return the URI components of the URI
* @since 4.1.5
*/
public static UriComponentsBuilder fromHttpRequest(HttpRequest request) {
return fromUri(request.getURI()).adaptFromForwardedHeaders(request.getHeaders());
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public ListenableFuture<ClientHttpResponse> executeAsync(HttpRequest request, byte[] body)
throws IOException {
if (this.iterator.hasNext()) {
AsyncClientHttpRequestInterceptor interceptor = this.iterator.next();
return interceptor.intercept(request, body, this);
}
else {
URI uri = request.getURI();
HttpMethod method = request.getMethod();
HttpHeaders headers = request.getHeaders();
Assert.state(method != null, "No standard HTTP method");
AsyncClientHttpRequest delegate = requestFactory.createAsyncRequest(uri, method);
delegate.getHeaders().putAll(headers);
if (body.length > 0) {
StreamUtils.copy(body, delegate.getBody());
}
return delegate.executeAsync();
}
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithoutBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(Collections.singletonList(interceptor));
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException {
if (this.iterator.hasNext()) {
ClientHttpRequestInterceptor nextInterceptor = this.iterator.next();
return nextInterceptor.intercept(request, body, this);
}
else {
HttpMethod method = request.getMethod();
Assert.state(method != null, "No standard HTTP method");
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
if (body.length > 0) {
if (delegate instanceof StreamingHttpOutputMessage) {
StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate;
streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream));
}
else {
StreamUtils.copy(body, delegate.getBody());
}
}
return delegate.execute();
}
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-15066
public void requestInterceptorCanAddExistingHeaderValueWithBody() throws Exception {
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add("MyHeader", "MyInterceptorValue");
return execution.execute(request, body);
};
template.setInterceptors(Collections.singletonList(interceptor));
MediaType contentType = MediaType.TEXT_PLAIN;
given(converter.canWrite(String.class, contentType)).willReturn(true);
HttpHeaders requestHeaders = new HttpHeaders();
mockSentRequest(POST, "http://example.com", requestHeaders);
mockResponseStatus(HttpStatus.OK);
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(contentType);
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("http://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
verify(response).close();
}
代码示例来源:origin: stagemonitor/stagemonitor
@Test
public void testB3HeaderContextPropagation() throws Exception {
HttpRequest httpRequest = new MockClientHttpRequest(HttpMethod.GET, new URI("http://example.com/foo?bar=baz"));
new SpringRestTemplateContextPropagatingInterceptor(tracingPlugin)
.intercept(httpRequest, null, mock(ClientHttpRequestExecution.class));
assertThat(httpRequest.getHeaders()).containsKey(B3HeaderFormat.SPAN_ID_NAME);
assertThat(httpRequest.getHeaders()).containsKey(B3HeaderFormat.TRACE_ID_NAME);
assertThat(mockTracer.finishedSpans()).hasSize(1);
assertThat(mockTracer.finishedSpans().get(0).operationName()).isEqualTo("GET http://example.com/foo");
}
代码示例来源:origin: openzipkin/brave
@Override public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request,
byte[] body, AsyncClientHttpRequestExecution execution) throws IOException {
Span span = handler.handleSend(injector, request.getHeaders(), request);
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
ListenableFuture<ClientHttpResponse> result = execution.executeAsync(request, body);
result.addCallback(new TraceListenableFutureCallback(span, handler));
return result;
} catch (IOException | RuntimeException | Error e) {
handler.handleReceive(null, e, span);
throw e;
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-8809
public void interceptor() throws Exception {
final String headerName = "MyHeader";
final String headerValue = "MyValue";
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
request.getHeaders().add(headerName, headerValue);
return execution.execute(request, body);
};
InterceptingClientHttpRequestFactory factory = new InterceptingClientHttpRequestFactory(
createRequestFactory(), Collections.singletonList(interceptor));
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.GET);
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
HttpHeaders responseHeaders = response.getHeaders();
assertEquals("Custom header invalid", headerValue, responseHeaders.getFirst(headerName));
}
finally {
if (response != null) {
response.close();
}
}
}
代码示例来源:origin: spring-projects/spring-framework
assertEquals(HttpMethod.GET, request.getMethod());
assertEquals(URI.create(this.server.url(path).toString()), request.getURI());
assertNotNull(request.getHeaders());
})
.verify(Duration.ofSeconds(3));
内容来源于网络,如有侵权,请联系作者删除!