本文整理汇总了Java中org.apache.http.protocol.HttpContext类的典型用法代码示例。如果您正苦于以下问题:Java HttpContext类的具体用法?Java HttpContext怎么用?Java HttpContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpContext类属于org.apache.http.protocol包,在下文中一共展示了HttpContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getKeepAliveDuration
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
try {
return Long.parseLong(value) * 1000;
} catch(NumberFormatException ignore) {
}
}
}
return -1;
}
示例2: execute
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
public HttpResponse execute(HttpHost target, HttpRequest request,
HttpContext context) throws IOException {
for (int c = 1;; c++) {
HttpResponse response = backend.execute(target, request, context);
if (retryStrategy.retryRequest(response, c, context)) {
long nextInterval = retryStrategy.getRetryInterval();
try {
log.trace("Wait for " + nextInterval);
Thread.sleep(nextInterval);
} catch (InterruptedException e) {
throw new InterruptedIOException(e.getMessage());
}
} else {
return response;
}
}
}
示例3: sendRequest
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
/**
* Puts a new request in queue as a new thread in pool to be executed
*
* @param client HttpClient to be used for request, can differ in single requests
* @param contentType MIME body type, for POST and PUT requests, may be null
* @param context Context of Android application, to hold the reference of request
* @param httpContext HttpContext in which the request will be executed
* @param responseHandler ResponseHandler or its subclass to put the response into
* @param uriRequest instance of HttpUriRequest, which means it must be of HttpDelete,
* HttpPost, HttpGet, HttpPut, etc.
* @return RequestHandle of future request process
*/
protected RequestHandle sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, ResponseHandlerInterface responseHandler, Context context) {
if (contentType != null) {
uriRequest.addHeader("Content-Type", contentType);
}
responseHandler.setRequestHeaders(uriRequest.getAllHeaders());
responseHandler.setRequestURI(uriRequest.getURI());
Future<?> request = threadPool.submit(new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler));
if (context != null) {
// Add request to request map
List<WeakReference<Future<?>>> requestList = requestMap.get(context);
if (requestList == null) {
requestList = new LinkedList<WeakReference<Future<?>>>();
requestMap.put(context, requestList);
}
requestList.add(new WeakReference<Future<?>>(request));
// TODO: Remove dead weakrefs from requestLists?
}
return new RequestHandle(request);
}
示例4: getKeepAliveDuration
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
while (it.hasNext()) {
HeaderElement he = it.nextElement();
String param = he.getName();
String value = he.getValue();
if (value != null && param.equalsIgnoreCase("timeout")) {
long timeout = Long.parseLong(value) * 1000;
if (timeout > 20 * 1000) {
return 20 * 1000;
} else {
return timeout;
}
}
}
return 5 * 1000;
}
示例5: execute
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public CloseableHttpResponse execute(
final HttpUriRequest request,
final HttpContext context) throws IOException, ClientProtocolException {
Subsegment subsegment = recorder.beginSubsegment(determineTarget(request).getHostName());
return wrapHttpSupplier(subsegment, () -> {
if (null != subsegment) {
TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request));
}
CloseableHttpResponse response = wrappedClient.execute(request, context);
if (null != subsegment) {
TracedResponseHandler.addResponseInformation(subsegment, response);
}
return response;
});
}
示例6: process
import org.apache.http.protocol.HttpContext; //导入依赖的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));
}
}
示例7: process
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
public void process(HttpRequest request, HttpContext context) throws HttpException,
IOException {
AuthState authState = (AuthState) context.getAttribute("http.auth.target-scope");
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute("http.auth" +
".credentials-provider");
HttpHost targetHost = (HttpHost) context.getAttribute("http.target_host");
if (authState.getAuthScheme() == null) {
Credentials creds = credsProvider.getCredentials(new AuthScope(targetHost.getHostName
(), targetHost.getPort()));
if (creds != null) {
authState.setAuthScheme(new BasicScheme());
authState.setCredentials(creds);
}
}
}
示例8: execute
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public CloseableHttpResponse execute(HttpUriRequest request, HttpContext context) throws IOException, ClientProtocolException {
Subsegment subsegment = getRecorder().beginSubsegment(TracedHttpClient.determineTarget(request).getHostName());
try {
if (null != subsegment) {
TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request));
}
CloseableHttpResponse response = super.execute(request, context);
if (null != subsegment) {
TracedResponseHandler.addResponseInformation(subsegment, response);
}
return response;
} catch (Exception e) {
if (null != subsegment) {
subsegment.addException(e);
}
throw e;
} finally {
if (null != subsegment) {
getRecorder().endSubsegment();
}
}
}
示例9: determineProxy
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool,
CrawlerSession crawlerSession) {
HttpClientContext httpClientContext = HttpClientContext.adapt(context);
Proxy proxy = (Proxy) crawlerSession.getExtInfo(VSCRAWLER_AVPROXY_KEY);
if (proxy == null) {
String accessUrl = null;
if (request instanceof HttpRequestWrapper || request instanceof HttpGet) {
accessUrl = HttpUriRequest.class.cast(request).getURI().toString();
}
if (!PoolUtil.isDungProxyEnabled(httpClientContext)) {
log.info("{}不会被代理", accessUrl);
return null;
}
proxy = ipPool.getIP(host.getHostName(), accessUrl);
if (proxy == null) {
return null;
}
crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy);
}
return proxy;
}
示例10: process
import org.apache.http.protocol.HttpContext; //导入依赖的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);
}
示例11: isAuthenticationRequested
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
public boolean isAuthenticationRequested(
final HttpHost host,
final HttpResponse response,
final AuthenticationStrategy authStrategy,
final AuthState authState,
final HttpContext context) {
if (authStrategy.isAuthenticationRequested(host, response, context)) {
return true;
} else {
switch (authState.getState()) {
case CHALLENGED:
case HANDSHAKE:
authState.setState(AuthProtocolState.SUCCESS);
authStrategy.authSucceeded(host, authState.getAuthScheme(), context);
break;
case SUCCESS:
break;
default:
authState.setState(AuthProtocolState.UNCHALLENGED);
}
return false;
}
}
示例12: connectSocket
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public Socket connectSocket(
final int connectTimeout,
final Socket socket, final HttpHost host,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpContext context
) throws IOException {
Socket socket0 = socket != null ? socket : createSocket(context);
if (localAddress != null) {
socket0.bind(localAddress);
}
try {
socket0.connect(remoteAddress, connectTimeout);
} catch (SocketTimeoutException e) {
throw new ConnectTimeoutException(e, host, remoteAddress.getAddress());
}
return socket0;
}
示例13: connectSocket
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Override
public Socket connectSocket(
final int connectTimeout,
final Socket socket,
final HttpHost host,
final InetSocketAddress remoteAddress,
final InetSocketAddress localAddress,
final HttpContext context) throws IOException {
if (log.isDebugEnabled()) {
log.debug("Connecting to {}:{}", remoteAddress.getAddress(), remoteAddress.getPort());
}
Socket connectedSocket = super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
if (connectedSocket instanceof SSLSocket) {
return new SdkSslSocket((SSLSocket) connectedSocket);
}
return new SdkSocket(connectedSocket);
}
示例14: createHttpContext
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
protected HttpContext createHttpContext() {
HttpContext context = new BasicHttpContext();
context.setAttribute(
ClientContext.SCHEME_REGISTRY,
getConnectionManager().getSchemeRegistry());
context.setAttribute(
ClientContext.AUTHSCHEME_REGISTRY,
getAuthSchemes());
context.setAttribute(
ClientContext.COOKIESPEC_REGISTRY,
getCookieSpecs());
context.setAttribute(
ClientContext.COOKIE_STORE,
getCookieStore());
context.setAttribute(
ClientContext.CREDS_PROVIDER,
getCredentialsProvider());
return context;
}
示例15: testExecute_happy
import org.apache.http.protocol.HttpContext; //导入依赖的package包/类
@Test
public void testExecute_happy() throws IOException {
Map<String, String> headers = new HashMap<>();
headers.put("Account-Id", "fubar");
headers.put("Content-Type", "application/json");
GenericApiGatewayResponse response = client.execute(
new GenericApiGatewayRequestBuilder()
.withBody(new ByteArrayInputStream("test request".getBytes()))
.withHttpMethod(HttpMethodName.POST)
.withHeaders(headers)
.withResourcePath("/test/orders").build());
assertEquals("Wrong response body", "test payload", response.getBody());
assertEquals("Wrong response status", 200, response.getHttpResponse().getStatusCode());
Mockito.verify(mockClient, times(1)).execute(argThat(new LambdaMatcher<>(
x -> (x.getMethod().equals("POST")
&& x.getFirstHeader("Account-Id").getValue().equals("fubar")
&& x.getFirstHeader("x-api-key").getValue().equals("12345")
&& x.getFirstHeader("Authorization").getValue().startsWith("AWS4")
&& x.getURI().toString().equals("https://foobar.execute-api.us-east-1.amazonaws.com/test/orders")))),
any(HttpContext.class));
}