本文整理匯總了Java中org.apache.http.client.protocol.HttpClientContext.setRequestConfig方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpClientContext.setRequestConfig方法的具體用法?Java HttpClientContext.setRequestConfig怎麽用?Java HttpClientContext.setRequestConfig使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.client.protocol.HttpClientContext
的用法示例。
在下文中一共展示了HttpClientContext.setRequestConfig方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testSocketTimeoutExistingConnection
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testSocketTimeoutExistingConnection() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpClientContext context = new HttpClientContext();
final RequestConfig config = RequestConfig.custom().setSocketTimeout(3000).build();
final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
context.setRequestConfig(config);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
Mockito.when(managedConn.isOpen()).thenReturn(true);
Mockito.when(requestExecutor.execute(
Mockito.same(request),
Mockito.<HttpClientConnection>any(),
Mockito.<HttpClientContext>any())).thenReturn(response);
mainClientExec.execute(route, request, context, execAware);
Mockito.verify(managedConn).setSocketTimeout(3000);
}
示例2: testSocketTimeoutExistingConnection
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testSocketTimeoutExistingConnection() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpClientContext context = new HttpClientContext();
final RequestConfig config = RequestConfig.custom().setSocketTimeout(3000).build();
final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
context.setRequestConfig(config);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
Mockito.when(managedConn.isOpen()).thenReturn(true);
Mockito.when(requestExecutor.execute(
Mockito.same(request),
Mockito.<HttpClientConnection>any(),
Mockito.<HttpClientContext>any())).thenReturn(response);
minimalClientExec.execute(route, request, context, execAware);
Mockito.verify(managedConn).setSocketTimeout(3000);
}
示例3: testExecuteLocalContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Test
public void testExecuteLocalContext() throws Exception {
final HttpGet httpget = new HttpGet("http://somehost/stuff");
final HttpClientContext context = HttpClientContext.create();
final Lookup<CookieSpecProvider> localCookieSpecRegistry = Mockito.mock(Lookup.class);
final Lookup<AuthSchemeProvider> localAuthSchemeRegistry = Mockito.mock(Lookup.class);
final CookieStore localCookieStore = Mockito.mock(CookieStore.class);
final CredentialsProvider localCredentialsProvider = Mockito.mock(CredentialsProvider.class);
final RequestConfig localConfig = RequestConfig.custom().build();
context.setCookieSpecRegistry(localCookieSpecRegistry);
context.setAuthSchemeRegistry(localAuthSchemeRegistry);
context.setCookieStore(localCookieStore);
context.setCredentialsProvider(localCredentialsProvider);
context.setRequestConfig(localConfig);
client.execute(httpget, context);
Assert.assertSame(localCookieSpecRegistry, context.getCookieSpecRegistry());
Assert.assertSame(localAuthSchemeRegistry, context.getAuthSchemeRegistry());
Assert.assertSame(localCookieStore, context.getCookieStore());
Assert.assertSame(localCredentialsProvider, context.getCredentialsProvider());
Assert.assertSame(localConfig, context.getRequestConfig());
}
示例4: testViaProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testViaProxy() throws Exception {
final HttpHost target = new HttpHost("somehost", 80, "http");
final HttpHost proxy = new HttpHost("proxy", 8080);
final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
final HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build());
final HttpRoute route = routePlanner.determineRoute(target, request, context);
Assert.assertEquals(target, route.getTargetHost());
Assert.assertEquals(proxy, route.getProxyHost());
Assert.assertEquals(2, route.getHopCount());
Assert.assertFalse(route.isSecure());
Mockito.verify(schemePortResolver, Mockito.never()).resolve(Mockito.<HttpHost>any());
}
示例5: testExecRequestNonPersistentConnection
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testExecRequestNonPersistentConnection() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpClientContext context = new HttpClientContext();
final RequestConfig config = RequestConfig.custom()
.setConnectTimeout(123)
.setSocketTimeout(234)
.setConnectionRequestTimeout(345)
.build();
context.setRequestConfig(config);
final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
Mockito.when(requestExecutor.execute(
Mockito.same(request),
Mockito.<HttpClientConnection>any(),
Mockito.<HttpClientContext>any())).thenReturn(response);
final CloseableHttpResponse finalResponse = mainClientExec.execute(
route, request, context, execAware);
Mockito.verify(connManager).requestConnection(route, null);
Mockito.verify(connRequest).get(345, TimeUnit.MILLISECONDS);
Mockito.verify(execAware, Mockito.times(1)).setCancellable(connRequest);
Mockito.verify(execAware, Mockito.times(2)).setCancellable(Mockito.<Cancellable>any());
Mockito.verify(connManager).connect(managedConn, route, 123, context);
Mockito.verify(connManager).routeComplete(managedConn, route, context);
Mockito.verify(managedConn).setSocketTimeout(234);
Mockito.verify(requestExecutor, Mockito.times(1)).execute(request, managedConn, context);
Mockito.verify(managedConn, Mockito.times(1)).close();
Mockito.verify(connManager).releaseConnection(managedConn, null, 0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(context.getTargetAuthState());
Assert.assertNotNull(context.getProxyAuthState());
Assert.assertSame(managedConn, context.getConnection());
Assert.assertNull(context.getUserToken());
Assert.assertNotNull(finalResponse);
Assert.assertTrue(finalResponse instanceof HttpResponseProxy);
}
示例6: testMaxRedirect
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test(expected = RedirectException.class)
public void testMaxRedirect() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpGet get = new HttpGet("/test");
final HttpRequestWrapper request = HttpRequestWrapper.wrap(get);
final HttpClientContext context = HttpClientContext.create();
final RequestConfig config = RequestConfig.custom()
.setRedirectsEnabled(true)
.setMaxRedirects(3)
.build();
context.setRequestConfig(config);
final CloseableHttpResponse response1 = Mockito.mock(CloseableHttpResponse.class);
final HttpGet redirect = new HttpGet("http://localhost:80/redirect");
Mockito.when(requestExecutor.execute(
Mockito.eq(route),
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpClientContext>any(),
Mockito.<HttpExecutionAware>any())).thenReturn(response1);
Mockito.when(redirectStrategy.isRedirected(
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpResponse>any(),
Mockito.<HttpClientContext>any())).thenReturn(Boolean.TRUE);
Mockito.when(redirectStrategy.getRedirect(
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpResponse>any(),
Mockito.<HttpClientContext>any())).thenReturn(redirect);
Mockito.when(httpRoutePlanner.determineRoute(
Mockito.eq(target),
Mockito.<HttpRequestWrapper>any(),
Mockito.<HttpClientContext>any())).thenReturn(route);
redirectExec.execute(route, request, context, execAware);
}
示例7: testExecRequestNonPersistentConnection
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testExecRequestNonPersistentConnection() throws Exception {
final HttpRoute route = new HttpRoute(target);
final HttpClientContext context = new HttpClientContext();
final RequestConfig config = RequestConfig.custom()
.setConnectTimeout(123)
.setSocketTimeout(234)
.setConnectionRequestTimeout(345)
.build();
context.setRequestConfig(config);
final HttpRequestWrapper request = HttpRequestWrapper.wrap(new HttpGet("http://bar/test"));
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
Mockito.when(requestExecutor.execute(
Mockito.same(request),
Mockito.<HttpClientConnection>any(),
Mockito.<HttpClientContext>any())).thenReturn(response);
final CloseableHttpResponse finalResponse = minimalClientExec.execute(
route, request, context, execAware);
Mockito.verify(connManager).requestConnection(route, null);
Mockito.verify(connRequest).get(345, TimeUnit.MILLISECONDS);
Mockito.verify(execAware, Mockito.times(1)).setCancellable(connRequest);
Mockito.verify(execAware, Mockito.times(2)).setCancellable(Mockito.<Cancellable>any());
Mockito.verify(connManager).connect(managedConn, route, 123, context);
Mockito.verify(connManager).routeComplete(managedConn, route, context);
Mockito.verify(managedConn).setSocketTimeout(234);
Mockito.verify(requestExecutor, Mockito.times(1)).execute(request, managedConn, context);
Mockito.verify(managedConn, Mockito.times(1)).close();
Mockito.verify(connManager).releaseConnection(managedConn, null, 0, TimeUnit.MILLISECONDS);
Assert.assertSame(managedConn, context.getConnection());
Assert.assertNotNull(finalResponse);
Assert.assertTrue(finalResponse instanceof HttpResponseProxy);
}
示例8: testCustomAuthPreference
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testCustomAuthPreference() throws Exception {
final TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC))
.build();
final HttpHost authhost = new HttpHost("somehost", 80);
final HttpClientContext context = HttpClientContext.create();
final Map<String, Header> challenges = new HashMap<String, Header>();
challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
.register("basic", new BasicSchemeFactory())
.register("digest", new DigestSchemeFactory()).build();
context.setAuthSchemeRegistry(authSchemeRegistry);
context.setRequestConfig(config);
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope("somehost", 80),
new UsernamePasswordCredentials("user", "pwd"));
context.setCredentialsProvider(credentialsProvider);
final Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
Assert.assertNotNull(options);
Assert.assertEquals(1, options.size());
final AuthOption option1 = options.remove();
Assert.assertTrue(option1.getAuthScheme() instanceof BasicScheme);
}
示例9: testGetLocationUriRelativeLocationNotAllowed
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test(expected=ProtocolException.class)
public void testGetLocationUriRelativeLocationNotAllowed() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final RequestConfig config = RequestConfig.custom().setRelativeRedirectsAllowed(false).build();
context.setRequestConfig(config);
final HttpGet httpget = new HttpGet("http://localhost/");
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "/stuff");
redirectStrategy.getLocationURI(httpget, response, context);
}
示例10: testGetLocationUriAllowCircularRedirects
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testGetLocationUriAllowCircularRedirects() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(true).build();
context.setRequestConfig(config);
final URI uri1 = URI.create("http://localhost/stuff1");
final URI uri2 = URI.create("http://localhost/stuff2");
final URI uri3 = URI.create("http://localhost/stuff3");
final HttpGet httpget1 = new HttpGet("http://localhost/");
final HttpResponse response1 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response1.addHeader("Location", uri1.toASCIIString());
final HttpGet httpget2 = new HttpGet(uri1.toASCIIString());
final HttpResponse response2 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response2.addHeader("Location", uri2.toASCIIString());
final HttpGet httpget3 = new HttpGet(uri2.toASCIIString());
final HttpResponse response3 = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response3.addHeader("Location", uri3.toASCIIString());
Assert.assertEquals(uri1, redirectStrategy.getLocationURI(httpget1, response1, context));
Assert.assertEquals(uri2, redirectStrategy.getLocationURI(httpget2, response2, context));
Assert.assertEquals(uri3, redirectStrategy.getLocationURI(httpget3, response3, context));
final List<URI> uris = context.getRedirectLocations();
Assert.assertNotNull(uris);
Assert.assertTrue(uris.contains(uri1));
Assert.assertTrue(uris.contains(uri2));
Assert.assertTrue(uris.contains(uri3));
Assert.assertEquals(3, uris.size());
Assert.assertEquals(uri1, uris.get(0));
Assert.assertEquals(uri2, uris.get(1));
Assert.assertEquals(uri3, uris.get(2));
}
示例11: testGetLocationUriDisallowCircularRedirects
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test(expected=ProtocolException.class)
public void testGetLocationUriDisallowCircularRedirects() throws Exception {
final DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, new HttpHost("localhost"));
final HttpGet httpget = new HttpGet("http://localhost/stuff");
final RequestConfig config = RequestConfig.custom().setCircularRedirectsAllowed(false).build();
context.setRequestConfig(config);
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_MOVED_TEMPORARILY, "Redirect");
response.addHeader("Location", "http://localhost/stuff");
final URI uri = URI.create("http://localhost/stuff");
Assert.assertEquals(uri, redirectStrategy.getLocationURI(httpget, response, context));
redirectStrategy.getLocationURI(httpget, response, context);
}
示例12: testViaProxy
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Test
public void testViaProxy() throws Exception {
final HttpHost target = new HttpHost("somehost", 80, "http");
final HttpHost proxy = new HttpHost("custom.proxy.host", 8080);
final HttpRequest request = new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1);
final HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(RequestConfig.custom().setProxy(proxy).build());
final HttpRoute route = routePlanner.determineRoute(target, request, context);
Assert.assertEquals(target, route.getTargetHost());
Assert.assertEquals(proxy, route.getProxyHost());
Assert.assertEquals(2, route.getHopCount());
Assert.assertFalse(route.isSecure());
}
示例13: createHttpContext
import org.apache.http.client.protocol.HttpClientContext; //導入方法依賴的package包/類
@Override
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
HttpClientContext context = HttpClientContext.create();
context.setRequestConfig(getRequestConfig());
return context;
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:7,代碼來源:TestRestTemplate.java