use of org.apache.hc.client5.http.classic.methods.HttpPost 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.classic.methods.HttpPost in project metrics by dropwizard.
the class HttpClientMetricNameStrategiesTest method hostAndMethodWithoutNameInWrappedRequest.
@Test
public void hostAndMethodWithoutNameInWrappedRequest() throws URISyntaxException {
HttpRequest request = rewriteRequestURI(new HttpPost("http://my.host.com/whatever"));
assertThat(HOST_AND_METHOD.getNameFor(null, request), is("org.apache.hc.client5.http.classic.HttpClient.my.host.com.post-requests"));
}
use of org.apache.hc.client5.http.classic.methods.HttpPost in project metrics by dropwizard.
the class HttpClientMetricNameStrategiesTest method hostAndMethodWithNameInWrappedRequest.
@Test
public void hostAndMethodWithNameInWrappedRequest() throws URISyntaxException {
HttpRequest request = rewriteRequestURI(new HttpPost("http://my.host.com/whatever"));
assertThat(HOST_AND_METHOD.getNameFor("some-service", request), is("org.apache.hc.client5.http.classic.HttpClient.some-service.my.host.com.post-requests"));
}
use of org.apache.hc.client5.http.classic.methods.HttpPost in project weicoder by wdcode.
the class HttpUpload method upload.
/**
* 上传文件
*
* @param url post提交地址
* @param data 提交参数
* @param name 参数名
* @param b 流
* @return 返回结果
*/
public static String upload(String url, Map<String, Object> data, String name, byte[] b) {
// 如果文件为空
if (U.E.isEmpty(url) || U.E.isEmpty(b))
return StringConstants.EMPTY;
// 声明HttpPost
HttpPost post = null;
try {
// 获得HttpPost
post = new HttpPost(url);
post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, "multipart/form-data"));
// 多提交实体构造器
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 设置浏览器上传
builder.setMode(HttpMultipartMode.EXTENDED);
// 添加上传文件
builder.addBinaryBody(name, b);
// 参数
if (U.E.isNotEmpty(data))
// 设置参数
data.forEach((k, v) -> builder.addTextBody(k, W.C.toString(v)));
// 设置提交文件参数
post.setEntity(builder.build());
// 返回结果
return IOUtil.readString(HttpClient.CLIENT.execute(post).getEntity().getContent());
} catch (Exception e) {
LOG.error(e);
} finally {
// 销毁post
if (post != null)
post.abort();
}
return StringConstants.EMPTY;
}
use of org.apache.hc.client5.http.classic.methods.HttpPost in project weicoder by wdcode.
the class HttpAsyncClient method post.
/**
* 模拟post提交
*
* @param url post提交地址
* @param data 提交参数
* @param callback 回调结果
* @param charset 编码
*/
public static void post(String url, Map<String, Object> data, CallbackVoid<String> callback, String charset) {
// 声明HttpPost
HttpPost post = null;
try {
// 获得HttpPost
post = new HttpPost(url);
post.addHeader(new BasicHeader(HttpConstants.CONTENT_TYPE_KEY, HttpConstants.CONTENT_TYPE_VAL));
// 如果参数列表为空 data为空map
if (U.E.isNotEmpty(data)) {
// 声明参数列表
List<NameValuePair> list = Lists.newList(data.size());
// 设置参数
data.forEach((k, v) -> list.add(new BasicNameValuePair(k, W.C.toString(v))));
// 设置参数与 编码格式
post.setEntity(new UrlEncodedFormEntity(list));
}
// 执行POST
CLIENT.execute(SimpleRequestBuilder.copy(post).build(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void failed(Exception ex) {
LOG.error(ex);
}
@Override
public void completed(SimpleHttpResponse result) {
if (callback != null)
callback.callback(result.getBodyText());
}
@Override
public void cancelled() {
}
});
} catch (Exception e) {
LOG.error(e);
}
}
Aggregations