use of org.apache.http.client.config.RequestConfig in project mastering-java by Kingminghuang.
the class HttpClientCrawler method testTimeout.
private static void testTimeout() throws IOException {
int timeout = 15;
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
config(requestConfig);
HttpGet httpGet = new HttpGet("http://www.google.com:80");
HttpResponse response = client.execute(httpGet);
System.out.println(response.getStatusLine().getStatusCode());
}
use of org.apache.http.client.config.RequestConfig in project mastering-java by Kingminghuang.
the class HttpClientCrawler method testHardTimeout.
private static void testHardTimeout() throws IOException {
int hardTimeout = 15;
RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
config(requestConfig);
HttpGet httpGet = new HttpGet(EXAMPLE_URL);
TimerTask task = new TimerTask() {
@Override
public void run() {
if (httpGet != null) {
httpGet.abort();
}
}
};
new Timer(true).schedule(task, hardTimeout * 1000);
HttpResponse response = client.execute(httpGet);
System.out.println(response.getStatusLine().getStatusCode());
}
use of org.apache.http.client.config.RequestConfig in project mastering-java by Kingminghuang.
the class HttpClientCrawler method testStatusCode.
private static void testStatusCode() throws IOException {
RequestConfig requestConfig = RequestConfig.custom().setProxy(new HttpHost(DEFAULT_PROXY, DEFAULT_PORT, DEFAULT_SCHEMA)).build();
config(requestConfig);
HttpGet httpGet = new HttpGet(EXAMPLE_URL);
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode == HttpStatus.SC_OK);
}
use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.
the class ProxyController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
HttpGet method = new HttpGet(url);
method.setConfig(requestConfig);
InputStream in = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse resp = client.execute(method)) {
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
response.sendError(statusCode);
} else {
in = resp.getEntity().getContent();
IOUtils.copy(in, response.getOutputStream());
}
}
} finally {
IOUtils.closeQuietly(in);
}
return null;
}
use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.
the class VersionService method readLatestVersion.
/**
* Resolves the latest available Libresonic version by screen-scraping a web page.
*
* @throws IOException If an I/O error occurs.
*/
private void readLatestVersion() throws IOException {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion());
method.setConfig(requestConfig);
String content;
try (CloseableHttpClient client = HttpClients.createDefault()) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = client.execute(method, responseHandler);
}
Pattern finalPattern = Pattern.compile("LIBRESONIC_FULL_VERSION_BEGIN(.*)LIBRESONIC_FULL_VERSION_END");
Pattern betaPattern = Pattern.compile("LIBRESONIC_BETA_VERSION_BEGIN(.*)LIBRESONIC_BETA_VERSION_END");
try (BufferedReader reader = new BufferedReader(new StringReader(content))) {
String line = reader.readLine();
while (line != null) {
Matcher finalMatcher = finalPattern.matcher(line);
if (finalMatcher.find()) {
latestFinalVersion = new Version(finalMatcher.group(1));
LOG.info("Resolved latest Libresonic final version to: " + latestFinalVersion);
}
Matcher betaMatcher = betaPattern.matcher(line);
if (betaMatcher.find()) {
latestBetaVersion = new Version(betaMatcher.group(1));
LOG.info("Resolved latest Libresonic beta version to: " + latestBetaVersion);
}
line = reader.readLine();
}
}
}
Aggregations