org.apache.hc.client5.http.classic.methods.HttpGet Java Examples

The following examples show how to use org.apache.hc.client5.http.classic.methods.HttpGet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WebDriverManager.java    From webdrivermanager with Apache License 2.0 6 votes vote down vote up
protected InputStream openGitHubConnection(URL driverUrl)
        throws IOException {
    HttpGet get = httpClient.createHttpGet(driverUrl);

    String gitHubTokenName = config().getGitHubTokenName();
    String gitHubTokenSecret = config().getGitHubTokenSecret();
    if (!isNullOrEmpty(gitHubTokenName)
            && !isNullOrEmpty(gitHubTokenSecret)) {
        String userpass = gitHubTokenName + ":" + gitHubTokenSecret;
        String basicAuth = "Basic "
                + new String(new Base64().encode(userpass.getBytes()));
        get.addHeader("Authorization", basicAuth);
    }

    return httpClient.execute(get).getEntity().getContent();
}
 
Example #2
Source File: SpotifyHttpManager.java    From spotify-web-api-java with MIT License 6 votes vote down vote up
@Override
public String get(URI uri, Header[] headers) throws
  IOException,
  SpotifyWebApiException,
  ParseException {
  assert (uri != null);
  assert (!uri.toString().equals(""));

  final HttpGet httpGet = new HttpGet(uri);

  httpGet.setHeaders(headers);

  String responseBody = getResponseBody(execute(httpClientCaching, httpGet));

  httpGet.reset();

  return responseBody;
}
 
Example #3
Source File: Http5FileObject.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
protected InputStream doGetInputStream(final int bufferSize) throws Exception {
    final HttpGet getRequest = new HttpGet(getInternalURI());
    final ClassicHttpResponse httpResponse = executeHttpUriRequest(getRequest);
    final int status = httpResponse.getCode();

    if (status == HttpStatus.SC_NOT_FOUND) {
        throw new FileNotFoundException(getName());
    }

    if (status != HttpStatus.SC_OK) {
        throw new FileSystemException("vfs.provider.http/get.error", getName(), Integer.valueOf(status));
    }

    return new MonitoredHttpResponseContentInputStream(httpResponse, bufferSize);
}
 
Example #4
Source File: DemoIntegrationAT.java    From n2o-framework with Apache License 2.0 5 votes vote down vote up
/**
 * Проверка отдачи статики
 */
@Test
@Order(1)
public void checkStaticContent() throws IOException {
    HttpUriRequest request = new HttpGet("http://localhost:" + port + "/index.html");
    HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
    assertThat(httpResponse.getCode(), equalTo(HttpStatus.SC_OK));
}
 
Example #5
Source File: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #6
Source File: HttpClient.java    From webdrivermanager with Apache License 2.0 5 votes vote down vote up
public HttpGet createHttpGet(URL url) {
    HttpGet httpGet = new HttpGet(url.toString());
    httpGet.addHeader("user-agent", "Apache-HttpClient/5.0");
    httpGet.addHeader("accept-encoding", "gzip, deflate, br");
    httpGet.addHeader("cache-control", "max-age=0");

    RequestConfig requestConfig = custom().setCookieSpec(STRICT)
            .setConnectTimeout(config.getTimeout(), TimeUnit.SECONDS)
            .build();
    httpGet.setConfig(requestConfig);
    return httpGet;
}
 
Example #7
Source File: HttpUtils.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
public static String retrieveHttpAsString(String url) throws HttpException, IOException {
    RequestConfig requestConfig = RequestConfig.custom()
            .setResponseTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectTimeout(5000, TimeUnit.MILLISECONDS)
            .setConnectionRequestTimeout(5000, TimeUnit.MILLISECONDS)
            .setCookieSpec(StandardCookieSpec.IGNORE)
            .build();
    CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultRequestConfig(requestConfig)
            .setUserAgent(_userAgent)
            .build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setConfig(requestConfig);
    CloseableHttpResponse response = null;
    try {
        response = httpclient.execute(httpGet);
        final int statusCode = response.getCode();
        if (statusCode != HttpStatus.SC_OK) {
            throw new HttpException("Error " + statusCode + " for URL " + url);
        }
        HttpEntity entity = response.getEntity();
        String data = EntityUtils.toString(entity);
        EntityUtils.consume(entity);
        return data;
    } catch (IOException e) {
        throw new IOException("Error for URL " + url, e);
    } finally {
        if (response != null) {
            response.close();
        }
        httpclient.close();
    }
}
 
Example #8
Source File: Hc5HttpUtils.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HTTP GET Method.
 * 
 * @param url URL of the request.
 * @param properties Properties to drive the API.
 * @return GET Method
 * @throws URISyntaxException Exception if the URL is not correct.
 */
private static HttpGet createHttpGetMethod(
    String url,
    Map<String, String> properties) throws URISyntaxException {

  // Initialize URI
  StringBuilder debugUrl = (DEBUG_URL) ? new StringBuilder("GET  " + url) : null;
  URIBuilder uriBuilder = new URIBuilder(url, StandardCharsets.UTF_8);
  if (properties != null) {
    boolean first = true;
    Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> property = iter.next();
      String key = property.getKey();
      String value = property.getValue();
      uriBuilder.addParameter(key, value);
      first = fillDebugUrl(debugUrl, first, key, value);
    }
  }

  // Initialize GET Method
  HttpGet method = new HttpGet(uriBuilder.build());
  method.addHeader("Accept-Encoding", "gzip");

  if (DEBUG_URL && (debugUrl != null)) {
    debugText(debugUrl.toString());
  }
  return method;
}
 
Example #9
Source File: SpeedTestCallable.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
public void setHttpGet(HttpGet httpGet) {
    _httpGet = httpGet;
    _httpPost = null;
}
 
Example #10
Source File: SpeedTestCallable.java    From drftpd with GNU General Public License v2.0 4 votes vote down vote up
public void setHttpGet(HttpGet httpGet) {
    _httpGet = httpGet;
    _httpPost = null;
}
 
Example #11
Source File: StructurizrClient.java    From java with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the workspace with the given ID.
 *
 * @param workspaceId the workspace ID
 * @return a Workspace instance
 * @throws StructurizrClientException   if there are problems related to the network, authorization, JSON deserialization, etc
 */
public Workspace getWorkspace(long workspaceId) throws StructurizrClientException {
    if (workspaceId <= 0) {
        throw new IllegalArgumentException("The workspace ID must be a positive integer.");
    }

    try (CloseableHttpClient httpClient = HttpClients.createSystem()) {
        log.info("Getting workspace with ID " + workspaceId);
        HttpGet httpGet = new HttpGet(url + WORKSPACE_PATH + workspaceId);
        addHeaders(httpGet, "", "");
        debugRequest(httpGet, null);

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            debugResponse(response);

            String json = EntityUtils.toString(response.getEntity());
            if (response.getCode() == HttpStatus.SC_OK) {
                archiveWorkspace(workspaceId, json);

                if (encryptionStrategy == null) {
                    return new JsonReader().read(new StringReader(json));
                } else {
                    EncryptedWorkspace encryptedWorkspace = new EncryptedJsonReader().read(new StringReader(json));

                    if (encryptedWorkspace.getEncryptionStrategy() != null) {
                        encryptedWorkspace.getEncryptionStrategy().setPassphrase(encryptionStrategy.getPassphrase());
                        return encryptedWorkspace.getWorkspace();
                    } else {
                        // this workspace isn't encrypted, even though the client has an encryption strategy set
                        return new JsonReader().read(new StringReader(json));
                    }
                }
            } else {
                ApiResponse apiResponse = ApiResponse.parse(json);
                throw new StructurizrClientException(apiResponse.getMessage());
            }
        }
    } catch (Exception e) {
        log.error(e);
        throw new StructurizrClientException(e);
    }
}