Search in sources :

Example 21 with RequestConfig

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());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 22 with RequestConfig

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());
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) TimerTask(java.util.TimerTask) Timer(java.util.Timer) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 23 with RequestConfig

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);
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 24 with RequestConfig

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;
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 25 with RequestConfig

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();
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Version(org.libresonic.player.domain.Version) HttpGet(org.apache.http.client.methods.HttpGet) BasicResponseHandler(org.apache.http.impl.client.BasicResponseHandler)

Aggregations

RequestConfig (org.apache.http.client.config.RequestConfig)82 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)28 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)27 HttpGet (org.apache.http.client.methods.HttpGet)26 HttpPost (org.apache.http.client.methods.HttpPost)18 Test (org.junit.Test)15 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)13 StringEntity (org.apache.http.entity.StringEntity)13 IOException (java.io.IOException)12 WxError (me.chanjar.weixin.common.bean.result.WxError)11 URI (java.net.URI)9 HttpEntity (org.apache.http.HttpEntity)9 HttpResponse (org.apache.http.HttpResponse)9 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)9 InputStream (java.io.InputStream)8 HttpHost (org.apache.http.HttpHost)8 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)8 Configurable (org.apache.http.client.methods.Configurable)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 HttpClient (org.apache.http.client.HttpClient)6