本文整理汇总了Java中org.apache.http.HttpRequest类的典型用法代码示例。如果您正苦于以下问题:Java HttpRequest类的具体用法?Java HttpRequest怎么用?Java HttpRequest使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpRequest类属于org.apache.http包,在下文中一共展示了HttpRequest类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import org.apache.http.HttpRequest; //导入依赖的package包/类
/**
* Starts the HTTP server.
*
* @return the listening port.
*/
static int start () throws IOException
{
server = ServerBootstrap.bootstrap ().registerHandler ("*",
new HttpRequestHandler ()
{
@Override
public void handle (HttpRequest request, HttpResponse response,
HttpContext context)
throws HttpException, IOException
{
response.setStatusCode (HttpStatus.SC_OK);
response.setEntity (new StringEntity ("0123456789"));
}
})
.create ();
server.start ();
return server.getLocalPort ();
}
示例2: getRedirect
import org.apache.http.HttpRequest; //导入依赖的package包/类
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
URI uri = this.getLocationURI(request, response, context);
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
return new HttpHead(uri);
} else if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
return this.copyEntity(new HttpPost(uri), request);
} else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
return this.copyEntity(new HttpPut(uri), request);
} else if (method.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
return new HttpDelete(uri);
} else if (method.equalsIgnoreCase(HttpTrace.METHOD_NAME)) {
return new HttpTrace(uri);
} else if (method.equalsIgnoreCase(HttpOptions.METHOD_NAME)) {
return new HttpOptions(uri);
} else if (method.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
return this.copyEntity(new HttpPatch(uri), request);
} else {
return new HttpGet(uri);
}
}
示例3: RequestEntityRestStorageService
import org.apache.http.HttpRequest; //导入依赖的package包/类
public RequestEntityRestStorageService(final S3Session session,
final Jets3tProperties properties,
final HttpClientBuilder configuration) {
super(session.getHost().getCredentials().isAnonymousLogin() ? null :
new AWSCredentials(null, null) {
@Override
public String getAccessKey() {
return session.getHost().getCredentials().getUsername();
}
@Override
public String getSecretKey() {
return session.getHost().getCredentials().getPassword();
}
},
new PreferencesUseragentProvider().get(), null, properties);
this.session = session;
configuration.disableContentCompression();
configuration.setRetryHandler(new S3HttpRequestRetryHandler(this, preferences.getInteger("http.connections.retry")));
configuration.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if(response.containsHeader("x-amz-bucket-region")) {
final String host = ((HttpUriRequest) request).getURI().getHost();
if(!StringUtils.equals(session.getHost().getHostname(), host)) {
regionEndpointCache.putRegionForBucketName(
StringUtils.split(StringUtils.removeEnd(((HttpUriRequest) request).getURI().getHost(), session.getHost().getHostname()), ".")[0],
response.getFirstHeader("x-amz-bucket-region").getValue());
}
}
return super.getRedirect(request, response, context);
}
});
this.setHttpClient(configuration.build());
}
示例4: execute
import org.apache.http.HttpRequest; //导入依赖的package包/类
public <T> T execute(HttpHost target, HttpRequest request,
ResponseHandler<? extends T> responseHandler, HttpContext context)
throws IOException, ClientProtocolException {
HttpResponse response = execute(target, request, context);
try {
return responseHandler.handleResponse(response);
} finally {
HttpEntity entity = response.getEntity();
if (entity != null) EntityUtils.consume(entity);
}
}
示例5: afterPreCap
import org.apache.http.HttpRequest; //导入依赖的package包/类
@Override
public void afterPreCap(InvokeChainContext context, Object[] args) {
if (args.length < 2) {
return;
}
/**
* after precap the client's span is created, set the span meta into http request header
*/
String url = (String) context.get(InvokeChainConstants.CLIENT_SPAN_THREADLOCAL_STOREKEY);
Span span = this.spanFactory.getSpanFromContext(url);
String spanMeta = this.spanFactory.getSpanMeta(span);
HttpRequest request = (HttpRequest) args[1];
request.removeHeaders(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO);
request.addHeader(InvokeChainConstants.PARAM_HTTPHEAD_SPANINFO, spanMeta);
handleSlowOperSupporter(request, span, context);
}
示例6: process
import org.apache.http.HttpRequest; //导入依赖的package包/类
@Override
public void process(final HttpRequest request,
final HttpContext context) throws HttpException, IOException {
final HttpRequestAttachment.Builder builder = create("Request", request.getRequestLine().getUri())
.withMethod(request.getRequestLine().getMethod());
Stream.of(request.getAllHeaders())
.forEach(header -> builder.withHeader(header.getName(), header.getValue()));
if (request instanceof HttpEntityEnclosingRequest) {
final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
entity.writeTo(os);
final String body = new String(os.toByteArray(), StandardCharsets.UTF_8);
builder.withBody(body);
}
final HttpRequestAttachment requestAttachment = builder.build();
processor.addAttachment(requestAttachment, renderer);
}
示例7: doExecute
import org.apache.http.HttpRequest; //导入依赖的package包/类
@Override
protected CloseableHttpResponse doExecute(HttpHost httpHost, HttpRequest httpRequest, HttpContext httpContext) throws IOException {
synchronized (rulesUnderConstruction) {
for (RuleBuilder ruleBuilder : rulesUnderConstruction) {
rules.add(ruleBuilder.toRule());
}
rulesUnderConstruction.clear();
}
Request request = new Request(httpHost, httpRequest, httpContext);
requests.add(request);
Rule rule = rules.stream()
.filter(r -> r.matches(httpHost, httpRequest, httpContext))
.reduce((a, b) -> b)
.orElse(NOT_FOUND);
HttpResponse response = rule.nextResponse(request);
return new HttpResponseProxy(response);
}
示例8: isRedirectRequested
import org.apache.http.HttpRequest; //导入依赖的package包/类
public boolean isRedirectRequested(
final HttpResponse response,
final HttpContext context) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
switch (statusCode) {
case HttpStatus.SC_MOVED_TEMPORARILY:
case HttpStatus.SC_MOVED_PERMANENTLY:
case HttpStatus.SC_TEMPORARY_REDIRECT:
HttpRequest request = (HttpRequest) context.getAttribute(
ExecutionContext.HTTP_REQUEST);
String method = request.getRequestLine().getMethod();
return method.equalsIgnoreCase(HttpGet.METHOD_NAME)
|| method.equalsIgnoreCase(HttpHead.METHOD_NAME);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
示例9: execute
import org.apache.http.HttpRequest; //导入依赖的package包/类
@Override
public CloseableHttpResponse execute(
final HttpHost target,
final HttpRequest request,
final HttpContext context) throws IOException, ClientProtocolException {
Subsegment subsegment = recorder.beginSubsegment(target.getHostName());
return wrapHttpSupplier(subsegment, () -> {
if (null != subsegment) {
TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(target, request));
}
CloseableHttpResponse response = wrappedClient.execute(target, request, context);
if (null != subsegment) {
TracedResponseHandler.addResponseInformation(subsegment, response);
}
return response;
});
}
示例10: process
import org.apache.http.HttpRequest; //导入依赖的package包/类
public void process(HttpRequest request, HttpContext context) {
if (!request.containsHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING)) {
request.addHeader(AsyncHttpClient.HEADER_ACCEPT_ENCODING, AsyncHttpClient
.ENCODING_GZIP);
}
for (String header : AsyncHttpClient.access$000(this.this$0).keySet()) {
if (request.containsHeader(header)) {
Header overwritten = request.getFirstHeader(header);
Log.d(AsyncHttpClient.LOG_TAG, String.format("Headers were overwritten! (%s | %s)" +
" overwrites (%s | %s)", new Object[]{header, AsyncHttpClient.access$000
(this.this$0).get(header), overwritten.getName(), overwritten.getValue()}));
request.removeHeader(overwritten);
}
request.addHeader(header, (String) AsyncHttpClient.access$000(this.this$0).get(header));
}
}
示例11: getRedirect
import org.apache.http.HttpRequest; //导入依赖的package包/类
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
URI uri = getLocationURI(request, response, context);
String method = request.getRequestLine().getMethod();
if ("post".equalsIgnoreCase(method)) {
try {
HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
httpRequestWrapper.setURI(uri);
httpRequestWrapper.removeHeaders("Content-Length");
return httpRequestWrapper;
} catch (Exception e) {
logger.error("强转为HttpRequestWrapper出错");
}
return new HttpPost(uri);
} else {
return new HttpGet(uri);
}
}
示例12: authenticate
import org.apache.http.HttpRequest; //导入依赖的package包/类
/**
* Produces basic authorization header for the given set of {@link Credentials}.
*
* @param credentials The set of credentials to be used for authentication
* @param request The request being authenticated
* @throws InvalidCredentialsException if authentication credentials are not
* valid or not applicable for this authentication scheme
* @throws AuthenticationException if authorization string cannot
* be generated due to an authentication failure
*
* @return a basic authorization string
*/
@Override
public Header authenticate(
final Credentials credentials,
final HttpRequest request,
final HttpContext context) throws AuthenticationException {
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null");
}
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
String charset = AuthParams.getCredentialCharset(request.getParams());
return authenticate(credentials, charset, isProxy());
}
示例13: extract
import org.apache.http.HttpRequest; //导入依赖的package包/类
/**
* Extract context from headers or from active Span
*
* @param request http request
* @return extracted context
*/
private SpanContext extract(HttpRequest request) {
SpanContext spanContext = tracer.extract(Format.Builtin.HTTP_HEADERS,
new HttpTextMapExtractAdapter(request));
if (spanContext != null) {
return spanContext;
}
Span span = tracer.activeSpan();
if (span != null) {
return span.context();
}
return null;
}
开发者ID:opentracing-contrib,项目名称:java-elasticsearch-client,代码行数:22,代码来源:TracingHttpClientConfigCallback.java
示例14: createConnectRequest
import org.apache.http.HttpRequest; //导入依赖的package包/类
/**
* Creates the CONNECT request for tunnelling.
* Called by {@link #createTunnelToTarget createTunnelToTarget}.
*
* @param route the route to establish
* @param context the context for request execution
*
* @return the CONNECT request for tunnelling
*/
protected HttpRequest createConnectRequest(HttpRoute route,
HttpContext context) {
// see RFC 2817, section 5.2 and
// INTERNET-DRAFT: Tunneling TCP based protocols through
// Web proxy servers
HttpHost target = route.getTargetHost();
String host = target.getHostName();
int port = target.getPort();
if (port < 0) {
Scheme scheme = connManager.getSchemeRegistry().
getScheme(target.getSchemeName());
port = scheme.getDefaultPort();
}
StringBuilder buffer = new StringBuilder(host.length() + 6);
buffer.append(host);
buffer.append(':');
buffer.append(Integer.toString(port));
String authority = buffer.toString();
ProtocolVersion ver = HttpProtocolParams.getVersion(params);
HttpRequest req = new BasicHttpRequest
("CONNECT", authority, ver);
return req;
}
示例15: requestIsAborted
import org.apache.http.HttpRequest; //导入依赖的package包/类
/**
* @since 4.2
*/
protected boolean requestIsAborted(final HttpRequest request) {
HttpRequest req = request;
if (request instanceof RequestWrapper) { // does not forward request to original
req = ((RequestWrapper) request).getOriginal();
}
return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
}