use of org.apache.hc.client5.http.impl.classic.CloseableHttpResponse in project scoold by Erudika.
the class HttpUtils method isValidCaptcha.
/**
* @param token CAPTCHA
* @return boolean
*/
public static boolean isValidCaptcha(String token) {
if (StringUtils.isBlank(Config.getConfigParam("signup_captcha_secret_key", ""))) {
return true;
}
if (StringUtils.isBlank(token)) {
return false;
}
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("secret", Config.getConfigParam("signup_captcha_secret_key", "")));
params.add(new BasicNameValuePair("response", token));
HttpPost post = new HttpPost("https://www.google.com/recaptcha/api/siteverify");
post.setEntity(new UrlEncodedFormEntity(params));
try (CloseableHttpResponse resp = HttpUtils.getHttpClient().execute(post)) {
if (resp.getCode() == HttpStatus.SC_OK && resp.getEntity() != null) {
Map<String, Object> data = ParaObjectUtils.getJsonReader(Map.class).readValue(resp.getEntity().getContent());
if (data != null && data.containsKey("success")) {
return (boolean) data.getOrDefault("success", false);
}
}
} catch (Exception ex) {
LoggerFactory.getLogger(HttpUtils.class).debug("Failed to verify CAPTCHA: {}", ex.getMessage());
}
return false;
}
use of org.apache.hc.client5.http.impl.classic.CloseableHttpResponse in project OpenRefine by OpenRefine.
the class HttpClient method postNameValue.
public String postNameValue(String serviceUrl, String name, String value) throws IOException {
HttpPost request = new HttpPost(serviceUrl);
List<NameValuePair> body = Collections.singletonList(new BasicNameValuePair(name, value));
request.setEntity(new UrlEncodedFormEntity(body, StandardCharsets.UTF_8));
try (CloseableHttpResponse response = httpClient.execute(request)) {
String reasonPhrase = response.getReasonPhrase();
int statusCode = response.getCode();
if (statusCode >= 400) {
// We should never see 3xx since they get handled automatically
throw new IOException(String.format("HTTP error %d : %s for URL %s", statusCode, reasonPhrase, request.getRequestUri()));
}
return ParsingUtilities.inputStreamToString(response.getEntity().getContent());
}
}
Aggregations