下面列出了怎么用org.apache.http.impl.client.LaxRedirectStrategy的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Rest template setup including a disabled SSL certificate validation.
* @throws Exception in case of errors
*/
private static void setupRestTemplate() throws Exception {
final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;
final SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
final HttpClient httpClient = HttpClientBuilder.create()
.setRedirectStrategy(new LaxRedirectStrategy())
.setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
.build();
factory.setHttpClient(httpClient);
restTemplate.setRequestFactory(factory);
for (int i = 0; i < restTemplate.getMessageConverters().size(); i++) {
if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter) {
restTemplate.getMessageConverters().set(i, new StringHttpMessageConverter(StandardCharsets.UTF_8));
break;
}
}
}
/**
* 默认是 Bsic认证机制
*
* @param ip
* @param username
* @param password
* @return
*/
public static HttpClient getHtpClient(String ip, int port, String username, String password) {
HttpHost proxy = new HttpHost(ip, port);
Lookup<AuthSchemeProvider> authProviders =
RegistryBuilder.<AuthSchemeProvider>create()
.register(AuthSchemes.BASIC, new BasicSchemeFactory())
.build();
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
if (username != null && password != null) {
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
} else {
credsProvider.setCredentials(AuthScope.ANY, null);
}
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients
.custom()
.setConnectionManager(cm)
.setProxy(proxy)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(requestConfig)
.setDefaultAuthSchemeRegistry(authProviders)
.setDefaultCredentialsProvider(credsProvider)
.build();
return httpClient;
}
public static CrawlerHttpClientBuilder setDefault(CrawlerHttpClientBuilder proxyFeedBackDecorateHttpClientBuilder) {
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoLinger(-1).setSoReuseAddress(false)
.setSoTimeout(ProxyConstant.SOCKETSO_TIMEOUT).setTcpNoDelay(true).build();
proxyFeedBackDecorateHttpClientBuilder.setDefaultSocketConfig(socketConfig)
// .setSSLSocketFactory(sslConnectionSocketFactory)
// dungproxy0.0.6之后的版本,默认忽略https证书检查
.setRedirectStrategy(new LaxRedirectStrategy())
// 注意,这里使用ua生产算法自动产生ua,如果是mobile,可以使用
// com.virjar.vscrawler.core.net.useragent.UserAgentBuilder.randomAppUserAgent()
.setUserAgent(UserAgentBuilder.randomUserAgent())
// 对于爬虫来说,连接池没啥卵用,直接禁止掉(因为我们可能创建大量HttpClient,每个HttpClient一个连接池,会把系统socket资源撑爆)
// 测试开80个httpClient抓数据大概一个小时系统就会宕机
.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
// 现在有些网站的网络响应头部数据有非ASCII数据,httpclient默认只使用ANSI标准解析header,这可能导致带有中文的header数据无法解析
.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charsets.UTF_8).build());
return proxyFeedBackDecorateHttpClientBuilder;
}
public static String getGithubZipball(String owner, String repo, String destFolder) throws IOException {
CloseableHttpClient httpclient = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
String urlForGet = "https://api.github.com/repos/" + owner + "/" + repo + "/zipball/";
HttpGet get = new HttpGet(urlForGet);
HttpResponse response = httpclient.execute(get);
InputStream is = response.getEntity().getContent();
String filePath = destFolder + File.separator + repo + ".zip";
FileOutputStream fos = new FileOutputStream(new File(filePath));
int inByte;
while ((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
return filePath;
}
public static CloseableHttpClient createHttpClient(final int maxRedirects) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
s_logger.info("Creating new HTTP connection pool and client");
final Registry<ConnectionSocketFactory> socketFactoryRegistry = createSocketFactoryConfigration();
final BasicCookieStore cookieStore = new BasicCookieStore();
final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connManager.setDefaultMaxPerRoute(MAX_ALLOCATED_CONNECTIONS_PER_ROUTE);
connManager.setMaxTotal(MAX_ALLOCATED_CONNECTIONS);
final RequestConfig requestConfig = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setMaxRedirects(maxRedirects)
.setSocketTimeout(DEFAULT_SOCKET_TIMEOUT)
.setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT)
.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT)
.build();
return HttpClientBuilder.create()
.setConnectionManager(connManager)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(cookieStore)
.setRetryHandler(new StandardHttpRequestRetryHandler())
.build();
}
/**
* Creates a new builder instance. Callers should avoid invoking this until/unless they have validated that TLS
* functionality is needed.
*
* @throws IOException if the necessary clients could not be built, which may occur if the cluster doesn't
* support TLS
*/
public Builder(String serviceName, SchedulerConfig schedulerConfig) throws IOException {
this.serviceName = serviceName;
this.schedulerConfig = schedulerConfig;
this.namespace = schedulerConfig.getSecretsNamespace(serviceName);
DcosHttpExecutor executor = new DcosHttpExecutor(new DcosHttpClientBuilder()
.setTokenProvider(schedulerConfig.getDcosAuthTokenProvider())
.setRedirectStrategy(new LaxRedirectStrategy() {
protected boolean isRedirectable(String method) {
// Also treat PUT calls as redirectable
return method.equalsIgnoreCase(HttpPut.METHOD_NAME) || super.isRedirectable(method);
}
}));
this.tlsArtifactsUpdater = new TLSArtifactsUpdater(
serviceName, new SecretsClient(executor), new CertificateAuthorityClient(executor));
}
BaseParser(SubstitutionScheduleData scheduleData, CookieProvider cookieProvider) {
this.scheduleData = scheduleData;
this.cookieProvider = cookieProvider;
this.cookieStore = new BasicCookieStore();
this.colorProvider = new ColorProvider(scheduleData);
this.encodingDetector = new UniversalDetector(null);
this.debuggingDataHandler = new NoOpDebuggingDataHandler();
this.sardine = null;
try {
SSLConnectionSocketFactory sslsf = getSslConnectionSocketFactory(scheduleData);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
.build();
this.executor = Executor.newInstance(httpclient).use(cookieStore);
} catch (GeneralSecurityException | JSONException | IOException e) {
throw new RuntimeException(e);
}
}
private void connect() throws IOException {
String u = BASE_URI+"/login";
httpclient = HttpClients.custom().setUserAgent(MTGConstants.USER_AGENT).setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpEntity p = httpclient.execute(new HttpGet(u), httpContext).getEntity();
String token = URLTools.toHtml(EntityUtils.toString(p)).select("input[name=_token]").first().attr("value");
HttpPost login = new HttpPost(u);
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("email", getString("LOGIN")));
nvps.add(new BasicNameValuePair("password", getString("PASS")));
nvps.add(new BasicNameValuePair("remember", "on"));
nvps.add(new BasicNameValuePair("_token", token));
login.setEntity(new UrlEncodedFormEntity(nvps));
login.addHeader(URLTools.REFERER, u);
login.addHeader(URLTools.UPGR_INSECURE_REQ, "1");
login.addHeader(URLTools.ORIGIN, BASE_URI);
HttpResponse resp = httpclient.execute(login, httpContext);
logger.debug("Connection : " + resp.getStatusLine().getReasonPhrase());
}
protected CloseableHttpClient constructHttpClient() throws IOException {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20 * 1000)
.setConnectionRequestTimeout(20 * 1000)
.setSocketTimeout(20 * 1000)
.setMaxRedirects(20)
.build();
URL mmsc = new URL(apn.getMmsc());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (apn.hasAuthentication()) {
credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
}
return HttpClients.custom()
.setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
.setRedirectStrategy(new LaxRedirectStrategy())
.setUserAgent(SilencePreferences.getMmsUserAgent(context, USER_AGENT))
.setConnectionManager(new BasicHttpClientConnectionManager())
.setDefaultRequestConfig(config)
.setDefaultCredentialsProvider(credsProvider)
.build();
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
// Create a pool of HTTP connections to use to get the favicons
client = HttpClientBuilder.create()
.setConnectionManager(new PoolingHttpClientConnectionManager())
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
// Load the default favicon to use when no favicon was found of a remote host
try {
defaultBytes = Files.readAllBytes(Paths.get(JiveGlobals.getHomeDirectory(), "plugins/admin/webapp/images/server_16x16.gif"));
}
catch (final IOException e) {
LOGGER.warn("Unable to retrieve default favicon", e);
}
// Initialize caches.
missesCache = CacheFactory.createCache("Favicon Misses");
hitsCache = CacheFactory.createCache("Favicon Hits");
}
@Override
public HttpResponse Get(final String uri, final List<Cookie> cookies) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpGet get = new HttpGet(uri);
if (cookies != null) {
for (final Cookie c : cookies) {
client.getCookieStore().addCookie(c);
}
}
final HttpResponse response = client.execute(get);
return response;
// TODO Return something useful maybe... like an InputStream
}
public static CloseableHttpClient createHttpClientTrustAll()
throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.setSSLSocketFactory(sslContextFactory)
.setRedirectStrategy(new LaxRedirectStrategy()).build();
return httpclient;
}
@Before
public void beforePhotozExampleAdapterTest() throws Exception {
DroneUtils.addWebDriver(jsDriver);
this.deployer.deploy(RESOURCE_SERVER_ID);
clientPage.navigateTo();
// waitForPageToLoad();
assertCurrentUrlStartsWith(clientPage.toString());
testExecutor = JavascriptTestExecutorWithAuthorization.create(jsDriver, jsDriverTestRealmLoginPage);
clientPage.setTestExecutorPlayground(testExecutor, appServerContextRootPage + "/" + RESOURCE_SERVER_ID);
jsDriver.manage().deleteAllCookies();
try (CloseableHttpClient httpClient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build()) {
HttpGet request = new HttpGet(clientPage.toString() + "/unsecured/clean");
httpClient.execute(request).close();
}
}
private CloseableHttpClient buildCloseableHttpClient() {
return HttpClients
.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.disableAutomaticRetries()
.setDefaultRequestConfig(
RequestConfig
.custom()
.setConnectTimeout(defaultConnTimeout)
.setConnectionRequestTimeout(defaultConnMgrTimeout)
.setSocketTimeout(defaultSocketTimeout)
.build())
.build();
}
public static HttpClient getHtpClient() {
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients
.custom()
.setConnectionManager(cm)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(requestConfig)
.build();
return httpClient;
}
public static HttpClient getHtpClient(AuthenticationStrategy proxy) {
RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
CloseableHttpClient httpClient = HttpClients
.custom()
.setConnectionManager(cm)
.setProxyAuthenticationStrategy(proxy)
.setRedirectStrategy(new LaxRedirectStrategy())
.setDefaultRequestConfig(requestConfig)
.build();
return httpClient;
}
/**
* 描述:创建httpClient连接池,并初始化httpclient
*/
private void initHttpClient() throws ConfigurationException {
Configuration configuration = new PropertiesConfiguration(CONFIG_FILE);
//创建httpclient连接池
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager();
httpClientConnectionManager.setMaxTotal(configuration.getInt("http.max.total")); //设置连接池线程最大数量
httpClientConnectionManager.setDefaultMaxPerRoute(configuration.getInt("http.max.route")); //设置单个路由最大的连接线程数量
//创建http request的配置信息
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(configuration.getInt("http.request.timeout"))
.setSocketTimeout(configuration.getInt("http.socket.timeout"))
.setCookieSpec(CookieSpecs.DEFAULT).build();
//设置重定向策略
LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy() {
/**
* false 禁止重定向 true 允许
*/
@Override
public boolean isRedirected(HttpRequest request,
HttpResponse response, HttpContext context)
throws ProtocolException {
// TODO Auto-generated method stub
return isRediect ? super.isRedirected(request, response, context) : isRediect;
}
};
//初始化httpclient客户端
httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
.setDefaultRequestConfig(requestConfig)
//.setUserAgent(NewsConstant.USER_AGENT)
.setRedirectStrategy(redirectStrategy)
.build();
}
static ClientHttpRequestFactory usingHttpComponents(ClientOptions options, SslConfiguration sslConfiguration)
throws GeneralSecurityException, IOException {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setRoutePlanner(
new SystemDefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE, ProxySelector.getDefault()));
if (hasSslConfiguration(sslConfiguration)) {
SSLContext sslContext = getSSLContext(sslConfiguration, getTrustManagers(sslConfiguration));
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
httpClientBuilder.setSSLSocketFactory(sslSocketFactory);
httpClientBuilder.setSSLContext(sslContext);
}
RequestConfig requestConfig = RequestConfig.custom()
//
.setConnectTimeout(Math.toIntExact(options.getConnectionTimeout().toMillis())) //
.setSocketTimeout(Math.toIntExact(options.getReadTimeout().toMillis())) //
.setAuthenticationEnabled(true) //
.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
// Support redirects
httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
return new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build());
}
public static String getZdsZipball(String id, String slug, String type, String destFolder) throws IOException{
CloseableHttpClient httpclient = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
String urlForGet = "https://zestedesavoir.com/" + type + "/zip/"+ id + "/" + slug + ".zip";
log.debug("Tentative de téléchargement du lien "+urlForGet);
log.debug("Répertoire de téléchargement cible : "+destFolder);
HttpGet get = new HttpGet(urlForGet);
log.debug("Execution de la requete http");
HttpResponse response = httpclient.execute(get);
InputStream is = response.getEntity().getContent();
String filePath = destFolder + File.separator + slug + ".zip";
FileOutputStream fos = new FileOutputStream(new File(filePath));
log.debug("Début du téléchargement");
int inByte;
while ((inByte = is.read()) != -1)
fos.write(inByte);
is.close();
fos.close();
log.debug("Archive téléchargée : "+filePath);
return filePath;
}
private void check() {
try {
HttpUriRequest request = RequestBuilder
.get()
.setUri("http://th.tcpr.ca/thermos/version")
.addParameter("version", Thermos.getCurrentVersion()).build();
HttpResponse response = HttpClientBuilder.create()
.setUserAgent("Thermos Version Retriever")
.setRedirectStrategy(new LaxRedirectStrategy()).build()
.execute(request);
if (response.getStatusLine().getStatusCode() != 200) {
uncaughtException(mThread, new IllegalStateException(
"Status code isn't OK"));
return;
}
JSONObject json = (JSONObject) sParser.parse(new InputStreamReader(
response.getEntity().getContent()));
String version = (String) json.get("version");
if (!mUpToDateSupport || Thermos.getCurrentVersion() == null
|| !version.equals(Thermos.getCurrentVersion())) {
mCallback.newVersion(version);
} else {
mCallback.upToDate();
}
} catch (Exception e) {
uncaughtException(null, e);
}
}
private String read(String url) throws IOException {
logger.debug("retrieve from " + url);
HttpClient httpClient = HttpClients.custom().setUserAgent(MTGConstants.USER_AGENT)
.setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpGet req = new HttpGet(url);
req.addHeader("content-type", URLTools.HEADER_JSON);
HttpResponse resp = httpClient.execute(req);
return EntityUtils.toString(resp.getEntity());
}
private String read(String url) throws IOException {
logger.debug("retrieve events from " + url);
HttpClient httpClient = HttpClients.custom().setUserAgent(MTGConstants.USER_AGENT)
.setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpGet req = new HttpGet(url);
req.addHeader("content-type", URLTools.HEADER_JSON);
HttpResponse resp = httpClient.execute(req);
return EntityUtils.toString(resp.getEntity());
}
public CloseableHttpClient getAuthenticatedClient(CredentialsProvider creds) {
resetConnectionManager(httpPoolSize);
return HttpClients.custom()
.setDefaultCredentialsProvider(creds)
.setDefaultCookieStore(cookieStore)
.setConnectionManager(connectionManager)
.setConnectionManagerShared(true)
.setRedirectStrategy(new LaxRedirectStrategy())
.build();
}
@Override
public HttpResponse Get(final String uri, final Map<String, String> headers) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpGet get = new HttpGet(uri);
for (final String header : headers.keySet()) {
get.addHeader(header, headers.get(header));
}
final HttpResponse response = client.execute(get);
return response;
// TODO Return something useful maybe... like an InputStream
}
@Override
public HttpResponse Get(final String uri) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpGet get = new HttpGet(uri);
final HttpResponse response = client.execute(get);
return response;
// TODO Return something useful maybe... like an InputStream
}
@Override
public HttpResponse Get(final String uri, final String username, final String password) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpGet get = new HttpGet(uri);
final HttpResponse response = client.execute(get);
return response;
// TODO Return something useful maybe... like an InputStream
}
@Override
public HttpResponse Head(final String uri) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpHead head = new HttpHead(uri);
final HttpResponse response = client.execute(head);
return response;
}
@Override
public HttpResponse Post(final String uri, final HttpEntity httpEntity) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpPost post = new HttpPost(uri);
post.setEntity(httpEntity);
final HttpResponse response = client.execute(post);
return response;
}
@Override
public HttpResponse Post(final String uri, final HttpEntity httpEntity, final Header... header) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpPost post = new HttpPost(uri);
post.setEntity(httpEntity);
post.setHeaders(header);
final HttpResponse response = client.execute(post);
return response;
}
@Override
public HttpResponse Post(final String uri, final HttpEntity httpEntity,
final List<Cookie> cookies) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.setRedirectStrategy(new LaxRedirectStrategy());
final HttpPost post = new HttpPost(uri);
post.setEntity(httpEntity);
if (cookies != null) {
for (final Cookie c : cookies) {
client.getCookieStore().addCookie(c);
}
}
final HttpResponse response = client.execute(post);
return response;
}