本文整理汇总了Java中org.apache.http.HttpResponseInterceptor类的典型用法代码示例。如果您正苦于以下问题:Java HttpResponseInterceptor类的具体用法?Java HttpResponseInterceptor怎么用?Java HttpResponseInterceptor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpResponseInterceptor类属于org.apache.http包,在下文中一共展示了HttpResponseInterceptor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getHttpClient
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
private CloseableHttpClient getHttpClient() {
int timeout = 10000;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build();
return HttpClientBuilder
.create()
.useSystemProperties()
.addInterceptorFirst(new OutboundRequestIdSettingInterceptor())
.addInterceptorFirst((HttpRequestInterceptor) new OutboundRequestLoggingInterceptor())
.addInterceptorLast((HttpResponseInterceptor) new OutboundRequestLoggingInterceptor())
.setDefaultRequestConfig(config)
.build();
}
示例2: setupClient
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
/**
* This method is used to capture Location headers after HttpClient redirect handling.
*/
private void setupClient(final AbstractHttpClient client) {
this.client.addResponseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(final HttpResponse response, final HttpContext context)
throws HttpException, IOException {
Header header = response.getFirstHeader("Location");
if (header!=null) {
String location = header.getValue();
/*
* Append the base name to the Location header
*/
if (location.startsWith("/")) {
String baseUrl = context.getAttribute(ExecutionContext.HTTP_TARGET_HOST).toString();
location = baseUrl + location;
}
context.setAttribute("Location", location);
}
}
});
}
示例3: testContentEncodingRequestParameter
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testContentEncodingRequestParameter() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
final StringEntity original = new StringEntity("encoded stuff");
original.setContentEncoding("GZip");
response.setEntity(original);
final RequestConfig config = RequestConfig.custom()
.setDecompressionEnabled(false)
.build();
final HttpContext context = new BasicHttpContext();
context.setAttribute(HttpClientContext.REQUEST_CONFIG, config);
final HttpResponseInterceptor interceptor = new ResponseContentEncoding();
interceptor.process(response, context);
final HttpEntity entity = response.getEntity();
Assert.assertNotNull(entity);
Assert.assertFalse(entity instanceof GzipDecompressingEntity);
}
示例4: testParseCookies
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testParseCookies() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE, "name1=value1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
final Cookie cookie = cookies.get(0);
Assert.assertEquals(0, cookie.getVersion());
Assert.assertEquals("name1", cookie.getName());
Assert.assertEquals("value1", cookie.getValue());
Assert.assertEquals("localhost", cookie.getDomain());
Assert.assertEquals("/", cookie.getPath());
}
示例5: testNoCookieOrigin
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testNoCookieOrigin() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE, "name1=value1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, null);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(0, cookies.size());
}
示例6: testNoCookieSpec
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testNoCookieSpec() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE, "name1=value1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, null);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(0, cookies.size());
}
示例7: testNoCookieStore
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testNoCookieStore() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE, "name1=value1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, null);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(0, cookies.size());
}
示例8: testSetCookie2OverrideSetCookie
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testSetCookie2OverrideSetCookie() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE, "name1=value1");
response.addHeader(SM.SET_COOKIE2, "name1=value2; Version=1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertEquals(1, cookies.size());
final Cookie cookie = cookies.get(0);
Assert.assertEquals(1, cookie.getVersion());
Assert.assertEquals("name1", cookie.getName());
Assert.assertEquals("value2", cookie.getValue());
Assert.assertEquals("localhost.local", cookie.getDomain());
Assert.assertEquals("/", cookie.getPath());
}
示例9: testInvalidHeader
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testInvalidHeader() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE2, "name=value; Version=crap");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertTrue(cookies.isEmpty());
}
示例10: testCookieRejected
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void testCookieRejected() throws Exception {
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader(SM.SET_COOKIE2, "name=value; Domain=www.somedomain.com; Version=1");
final HttpClientContext context = HttpClientContext.create();
context.setAttribute(HttpClientContext.COOKIE_ORIGIN, this.cookieOrigin);
context.setAttribute(HttpClientContext.COOKIE_SPEC, this.cookieSpec);
context.setAttribute(HttpClientContext.COOKIE_STORE, this.cookieStore);
final HttpResponseInterceptor interceptor = new ResponseProcessCookies();
interceptor.process(response, context);
final List<Cookie> cookies = this.cookieStore.getCookies();
Assert.assertNotNull(cookies);
Assert.assertTrue(cookies.isEmpty());
}
示例11: RequestListenerThread
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
public RequestListenerThread(int port, final String docroot) throws IOException {
this.serversocket = new ServerSocket(port);
this.params = new SyncBasicHttpParams();
this.params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000).setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024).setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false).setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true).setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");
// Set up the HTTP protocol processor
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[]{
new ResponseDate(),
new ResponseServer(),
new ResponseContent(),
new ResponseConnControl()
});
// Set up request handlers
HttpRequestHandlerRegistry reqistry = new HttpRequestHandlerRegistry();
reqistry.register("*", new HttpFileHandler());
// Set up the HTTP service
this.httpService = new HttpService(
httpproc,
new DefaultConnectionReuseStrategy(),
new DefaultHttpResponseFactory(),
reqistry,
this.params);
}
示例12: RequestListenerThread
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
/**
* Default constructor which specifies the <code>port</code> to listen to
* for requests and the <code>handlers</code>, which specify how to handle
* the request.
*
* @param port
* the port to listen to
* @param handlers
* the handlers, which specify how to handle the different
* requests
*
* @throws IOException
* if some IO operation fails
*/
public RequestListenerThread(final int port,
final Map<String, IHandler> handlers) throws IOException {
super(port);
// Set up the HTTP protocol processor
final HttpProcessor httpproc = new ImmutableHttpProcessor(
new HttpResponseInterceptor[] { new ResponseDate(),
new ResponseServer(), new ResponseContent(),
new ResponseConnControl() });
// Set up request handlers
UriHttpRequestHandlerMapper registry = new UriHttpRequestHandlerMapper();
for (final Entry<String, IHandler> entry : handlers.entrySet()) {
registry.register(entry.getKey(), entry.getValue());
}
// Set up the HTTP service
httpService = new HttpService(httpproc, registry);
connFactory = DefaultBHttpServerConnectionFactory.INSTANCE;
}
示例13: getGzipResponseInterceptor
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
/**
* From apache examples
*
* @return
*
* @see <a
* href="http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientGZipContentCompression.java">Apache
* examples</a>
*/
private HttpResponseInterceptor getGzipResponseInterceptor() {
return new HttpResponseInterceptor() {
public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
HttpEntity entity = response.getEntity();
if (entity != null) {
Header ceheader = entity.getContentEncoding();
if (ceheader != null) {
HeaderElement[] codecs = ceheader.getElements();
for (int i = 0; i < codecs.length; i++) {
if (codecs[i].getName().equalsIgnoreCase("gzip")) {
response.setEntity(new GzipDecompressingEntity(response.getEntity()));
return;
}
}
}
}
}
};
}
示例14: interceptResponse
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
/**
* Tests if we can manipulate a response
* @throws Exception if anything goes wrong
*/
@Test
public void interceptResponse() throws Exception {
final AtomicBoolean interceptorCalled = new AtomicBoolean(false);
Download t = makeProjectAndTask();
t.src(makeSrc(INTERCEPTOR));
File dst = folder.newFile();
t.dest(dst);
t.responseInterceptor(new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context)
throws HttpException, IOException {
assertFalse(interceptorCalled.get());
interceptorCalled.set(true);
response.setEntity(new StringEntity(INTERCEPTED));
}
});
t.execute();
assertTrue(interceptorCalled.get());
String dstContents = FileUtils.readFileToString(dst);
assertEquals(INTERCEPTED, dstContents);
}
示例15: downloadingFiles
import org.apache.http.HttpResponseInterceptor; //导入依赖的package包/类
@Test
public void downloadingFiles() throws Exception {
ProxyServer bmp = new ProxyServer(8071);
bmp.start();
HttpResponseInterceptor downloader = new FileDownloader()
.addContentType("application/pdf");
bmp.addResponseInterceptor(downloader);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.PROXY, bmp.seleniumProxy());
WebDriver driver = new FirefoxDriver(caps);
driver.get("http://localhost/test_download.html");
driver.findElement(By.tagName("a")).click();
String fileName = driver.findElement(By.tagName("body")).getText();
assertTrue(new File(fileName).exists());
Thread.sleep(30000);
driver.quit();
bmp.stop();
}