本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.getTargetHost方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.getTargetHost方法的具體用法?Java HttpClientContext.getTargetHost怎麽用?Java HttpClientContext.getTargetHost使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.getTargetHost方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: resolveHttpRedirects
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
/** Return final location from http redirects */
public static String resolveHttpRedirects(String uri)
throws IOException, URISyntaxException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet(uri);
//httpget.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121202 Firefox/17.0 Iceweasel/17.0.1");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
return location.toString();
} finally {
response.close();
}
}
示例2: getServicePrincipalName
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private String getServicePrincipalName(final HttpContext context) {
final String spn;
if (this.servicePrincipalName != null) {
spn = this.servicePrincipalName;
} else {
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final HttpHost target = clientContext.getTargetHost();
if (target != null) {
spn = "HTTP/" + target.getHostName();
} else {
final RouteInfo route = clientContext.getHttpRoute();
if (route != null) {
spn = "HTTP/" + route.getTargetHost().getHostName();
} else {
// Should not happen
spn = null;
}
}
}
if (this.log.isDebugEnabled()) {
this.log.debug("Using SPN: " + spn);
}
return spn;
}
示例3: handleCacheMiss
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private CloseableHttpResponse handleCacheMiss(
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext context,
final HttpExecutionAware execAware) throws IOException, HttpException {
final HttpHost target = context.getTargetHost();
recordCacheMiss(target, request);
if (!mayCallBackend(request)) {
return Proxies.enhanceResponse(
new BasicHttpResponse(
HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout"));
}
final Map<String, Variant> variants = getExistingCacheVariants(target, request);
if (variants != null && !variants.isEmpty()) {
return negotiateResponseFromVariants(route, request, context,
execAware, variants);
}
return callBackend(route, request, context, execAware);
}
示例4: testBasicRedirect302
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect302() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_MOVED_TEMPORARILY));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/oldlocation/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(target, host);
}
示例5: testBasicRedirect303
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect303() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_SEE_OTHER));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/oldlocation/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(target, host);
}
示例6: testBasicRedirect307
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testBasicRedirect307() throws Exception {
this.serverBootstrap.registerHandler("*",
new BasicRedirectService(HttpStatus.SC_TEMPORARY_REDIRECT));
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final HttpGet httpget = new HttpGet("/oldlocation/");
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(target, host);
}
示例7: testRelativeRedirect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRedirect() throws Exception {
this.serverBootstrap.registerHandler("*", new RelativeRedirectService());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
final HttpGet httpget = new HttpGet("/oldlocation/");
httpget.setConfig(config);
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/relativelocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(host, target);
}
示例8: testRelativeRedirect2
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testRelativeRedirect2() throws Exception {
this.serverBootstrap.registerHandler("*", new RelativeRedirectService2());
final HttpHost target = start();
final HttpClientContext context = HttpClientContext.create();
final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(true).build();
final HttpGet httpget = new HttpGet("/test/oldlocation");
httpget.setConfig(config);
final HttpResponse response = this.httpclient.execute(target, httpget, context);
EntityUtils.consume(response.getEntity());
final HttpRequest reqWrapper = context.getRequest();
final HttpHost host = context.getTargetHost();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/test/relativelocation", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(host, target);
}
示例9: testUrlRedirect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private static void testUrlRedirect() throws IOException, URISyntaxException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
//String uri="http://feeds.nbcnews.com/c/35002/f/663303/s/410be3e0/sc/1/l/0L0Snbcnews0N0Cpolitics0Cfirst0Eread0Csimple0Ecommon0Esense0Ejeh0Ejohnson0Edefends0Eexecutive0Eaction0Eimmigration0En259736/story01.htm";
//String uri="http://feeds.theguardian.com/c/34708/f/663879/s/410c0702/sc/8/l/0L0Stheguardian0N0Cworld0C20A140Cdec0C0A20Cnorth0Ekorea0Esony0Ecyber0Eattack/story01.htm";
//String uri = "http://feeds.reuters.com/~r/Reuters/worldNews/~3/ySVJ_LFYBrs/story01.htm";
String uri = "http://rss.nytimes.com/c/34625/f/642565/s/40f27c2a/sc/20/l/0L0Snytimes0N0C20A140C110C290Cworld0Cmiddleeast0Cpalestinian0Ehaven0Efor0E60Edecades0Enow0Eflooded0Efrom0Esyria0E0Bhtml0Dpartner0Frss0Gemc0Frss/story01.htm";
HttpGet httpget = new HttpGet(uri);
httpget.addHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20121202 Firefox/17.0 Iceweasel/17.0.1");
CloseableHttpResponse response = httpclient.execute(httpget, context);
try {
HttpHost target = context.getTargetHost();
System.out.println("httpget URI: " + httpget.getURI());
System.out.println("target host: " + target);
System.out.println(response.getStatusLine().getStatusCode());
for (Header h : response.getAllHeaders()) {
System.out.println(h.getName()+ " : " + h.getValue());
}
// BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
// String line = null;
// while ((line = br.readLine()) != null) {
// System.out.println(line);
// }
List<URI> redirectLocations = context.getRedirectLocations();
if (redirectLocations != null)
for (URI loc : redirectLocations) {
System.out.println("redirect location: " + loc);
}
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
// Expected to be an absolute URI
} finally {
response.close();
}
}
示例10: needAuthentication
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private boolean needAuthentication(
final AuthStateHC4 targetAuthState,
final AuthStateHC4 proxyAuthState,
final HttpRoute route,
final HttpResponse response,
final HttpClientContext context) {
final RequestConfig config = context.getRequestConfig();
if (config.isAuthenticationEnabled()) {
HttpHost target = context.getTargetHost();
if (target == null) {
target = route.getTargetHost();
}
if (target.getPort() < 0) {
target = new HttpHost(
target.getHostName(),
route.getTargetHost().getPort(),
target.getSchemeName());
}
final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(
target, response, this.targetAuthStrategy, targetAuthState, context);
HttpHost proxy = route.getProxyHost();
// if proxy is not set use target host instead
if (proxy == null) {
proxy = route.getTargetHost();
}
final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(
proxy, response, this.proxyAuthStrategy, proxyAuthState, context);
if (targetAuthRequested) {
return this.authenticator.handleAuthChallenge(target, response,
this.targetAuthStrategy, targetAuthState, context);
}
if (proxyAuthRequested) {
return this.authenticator.handleAuthChallenge(proxy, response,
this.proxyAuthStrategy, proxyAuthState, context);
}
}
return false;
}
示例11: getLocationURI
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public URI getLocationURI(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
//get the location header to find out where to redirect to
final Header locationHeader = response.getFirstHeader("location");
if (locationHeader == null) {
// got a redirect response, but no location header
throw new ProtocolException(
"Received redirect response " + response.getStatusLine()
+ " but no location header");
}
final String location = locationHeader.getValue();
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Redirect requested to location '" + location + "'");
}
final RequestConfig config = clientContext.getRequestConfig();
URI uri = createLocationURI(location);
// rfc2616 demands the location value be a complete URI
// Location = "Location" ":" absoluteURI
try {
if (!uri.isAbsolute()) {
if (!config.isRelativeRedirectsAllowed()) {
throw new ProtocolException("Relative redirect location '"
+ uri + "' not allowed");
}
// Adjust location URI
final HttpHost target = clientContext.getTargetHost();
Asserts.notNull(target, "Target host");
final URI requestURI = new URI(request.getRequestLine().getUri());
final URI absoluteRequestURI = URIUtilsHC4.rewriteURI(requestURI, target, false);
uri = URIUtilsHC4.resolve(absoluteRequestURI, uri);
}
} catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
RedirectLocationsHC4 redirectLocations = (RedirectLocationsHC4) clientContext.getAttribute(
HttpClientContext.REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocationsHC4();
context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
}
if (!config.isCircularRedirectsAllowed()) {
if (redirectLocations.contains(uri)) {
throw new CircularRedirectException("Circular redirect to '" + uri + "'");
}
}
redirectLocations.add(uri);
return uri;
}
示例12: handleCacheHit
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private CloseableHttpResponse handleCacheHit(
final HttpRoute route,
final HttpRequestWrapper request,
final HttpClientContext context,
final HttpExecutionAware execAware,
final HttpCacheEntry entry) throws IOException, HttpException {
final HttpHost target = context.getTargetHost();
recordCacheHit(target, request);
CloseableHttpResponse out = null;
final Date now = getCurrentDate();
if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
log.debug("Cache hit");
out = generateCachedResponse(request, context, entry, now);
} else if (!mayCallBackend(request)) {
log.debug("Cache entry not suitable but only-if-cached requested");
out = generateGatewayTimeout(context);
} else if (!(entry.getStatusCode() == HttpStatus.SC_NOT_MODIFIED
&& !suitabilityChecker.isConditional(request))) {
log.debug("Revalidating cache entry");
return revalidateCacheEntry(route, request, context, execAware, entry, now);
} else {
log.debug("Cache entry not usable; calling backend");
return callBackend(route, request, context, execAware);
}
context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);
context.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
context.setAttribute(HttpCoreContext.HTTP_RESPONSE, out);
context.setAttribute(HttpCoreContext.HTTP_REQ_SENT, Boolean.TRUE);
return out;
}
示例13: handleBackendResponse
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
CloseableHttpResponse handleBackendResponse(
final HttpRequestWrapper request,
final HttpClientContext context,
final Date requestDate,
final Date responseDate,
final CloseableHttpResponse backendResponse) throws IOException {
log.trace("Handling Backend response");
responseCompliance.ensureProtocolCompliance(request, backendResponse);
final HttpHost target = context.getTargetHost();
final boolean cacheable = responseCachingPolicy.isResponseCacheable(request, backendResponse);
responseCache.flushInvalidatedCacheEntriesFor(target, request, backendResponse);
if (cacheable && !alreadyHaveNewerCacheEntry(target, request, backendResponse)) {
storeRequestIfModifiedSinceFor304Response(request, backendResponse);
return responseCache.cacheAndReturnResponse(target, request,
backendResponse, requestDate, responseDate);
}
if (!cacheable) {
try {
responseCache.flushCacheEntriesFor(target, request);
} catch (final IOException ioe) {
log.warn("Unable to flush invalid cache entries", ioe);
}
}
return backendResponse;
}
示例14: needAuthentication
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
private boolean needAuthentication(
final AuthState targetAuthState,
final AuthState proxyAuthState,
final HttpRoute route,
final HttpResponse response,
final HttpClientContext context) {
final RequestConfig config = context.getRequestConfig();
if (config.isAuthenticationEnabled()) {
HttpHost target = context.getTargetHost();
if (target == null) {
target = route.getTargetHost();
}
if (target.getPort() < 0) {
target = new HttpHost(
target.getHostName(),
route.getTargetHost().getPort(),
target.getSchemeName());
}
final boolean targetAuthRequested = this.authenticator.isAuthenticationRequested(
target, response, this.targetAuthStrategy, targetAuthState, context);
HttpHost proxy = route.getProxyHost();
// if proxy is not set use target host instead
if (proxy == null) {
proxy = route.getTargetHost();
}
final boolean proxyAuthRequested = this.authenticator.isAuthenticationRequested(
proxy, response, this.proxyAuthStrategy, proxyAuthState, context);
if (targetAuthRequested) {
return this.authenticator.handleAuthChallenge(target, response,
this.targetAuthStrategy, targetAuthState, context);
}
if (proxyAuthRequested) {
return this.authenticator.handleAuthChallenge(proxy, response,
this.proxyAuthStrategy, proxyAuthState, context);
}
}
return false;
}
示例15: getLocationURI
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
public URI getLocationURI(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
final HttpClientContext clientContext = HttpClientContext.adapt(context);
//get the location header to find out where to redirect to
final Header locationHeader = response.getFirstHeader("location");
if (locationHeader == null) {
// got a redirect response, but no location header
throw new ProtocolException(
"Received redirect response " + response.getStatusLine()
+ " but no location header");
}
final String location = locationHeader.getValue();
if (this.log.isDebugEnabled()) {
this.log.debug("Redirect requested to location '" + location + "'");
}
final RequestConfig config = clientContext.getRequestConfig();
URI uri = createLocationURI(location);
// rfc2616 demands the location value be a complete URI
// Location = "Location" ":" absoluteURI
try {
if (!uri.isAbsolute()) {
if (!config.isRelativeRedirectsAllowed()) {
throw new ProtocolException("Relative redirect location '"
+ uri + "' not allowed");
}
// Adjust location URI
final HttpHost target = clientContext.getTargetHost();
Asserts.notNull(target, "Target host");
final URI requestURI = new URI(request.getRequestLine().getUri());
final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
uri = URIUtils.resolve(absoluteRequestURI, uri);
}
} catch (final URISyntaxException ex) {
throw new ProtocolException(ex.getMessage(), ex);
}
RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
HttpClientContext.REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocations();
context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
}
if (!config.isCircularRedirectsAllowed()) {
if (redirectLocations.contains(uri)) {
throw new CircularRedirectException("Circular redirect to '" + uri + "'");
}
}
redirectLocations.add(uri);
return uri;
}