use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.
the class SimpleGetRequestExecutor method execute.
@Override
public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException {
if (queryParam != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? queryParam : '&' + queryParam;
}
HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
}
return responseContent;
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.
the class MaterialNewsInfoRequestExecutor method execute.
public WxMpMaterialNews execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String materialId) throws WxErrorException, ClientProtocolException, IOException {
HttpPost httpPost = new HttpPost(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpPost.setConfig(config);
}
Map<String, String> params = new HashMap<>();
params.put("media_id", materialId);
httpPost.setEntity(new StringEntity(WxGsonBuilder.create().toJson(params)));
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
WxError error = WxError.fromJson(responseContent);
if (error.getErrorCode() != 0) {
throw new WxErrorException(error);
} else {
return WxMpGsonBuilder.create().fromJson(responseContent, WxMpMaterialNews.class);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project weixin-java-tools by chanjarster.
the class QrCodeRequestExecutor method execute.
@Override
public File execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, WxMpQrCodeTicket ticket) throws WxErrorException, ClientProtocolException, IOException {
if (ticket != null) {
if (uri.indexOf('?') == -1) {
uri += '?';
}
uri += uri.endsWith("?") ? "ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8") : "&ticket=" + URLEncoder.encode(ticket.getTicket(), "UTF-8");
}
HttpGet httpGet = new HttpGet(uri);
if (httpProxy != null) {
RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
httpGet.setConfig(config);
}
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {
Header[] contentTypeHeader = response.getHeaders("Content-Type");
if (contentTypeHeader != null && contentTypeHeader.length > 0) {
// 出错
if (ContentType.TEXT_PLAIN.getMimeType().equals(contentTypeHeader[0].getValue())) {
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
throw new WxErrorException(WxError.fromJson(responseContent));
}
}
InputStream inputStream = InputStreamResponseHandler.INSTANCE.handleResponse(response);
File localFile = FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), "jpg");
return localFile;
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project dhis2-core by dhis2.
the class HttpUtils method httpPOST.
/**
* Method to make an HTTP POST call to a given URL with/without
* authentication.
*/
public static DhisHttpResponse httpPOST(String requestURL, Object body, boolean authorize, String username, String password, String contentType, int timeout) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
DhisHttpResponse dhisHttpResponse;
try {
HttpPost httpPost = new HttpPost(requestURL);
httpPost.setConfig(requestConfig);
if (body instanceof Map) {
@SuppressWarnings("unchecked") Map<String, String> parameters = (Map<String, String>) body;
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
if (parameter.getValue() != null) {
pairs.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
} else if (body instanceof String) {
httpPost.setEntity(new StringEntity((String) body));
}
if (!StringUtils.isNotEmpty(contentType)) {
httpPost.setHeader("Content-Type", contentType);
}
if (authorize) {
httpPost.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
}
HttpResponse response = httpClient.execute(httpPost);
log.info("Successfully got response from http POST.");
dhisHttpResponse = processResponse(requestURL, username, response);
} catch (Exception e) {
log.error("Exception occurred in httpPOST call with username " + username, e);
throw e;
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return dhisHttpResponse;
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.client.config.RequestConfig in project dhis2-core by dhis2.
the class HttpUtils method httpGET.
/**
* Method to make an HTTP GET call to a given URL with/without
* authentication.
*
* @throws Exception
* </pre>
*/
public static DhisHttpResponse httpGET(String requestURL, boolean authorize, String username, String password, Map<String, String> headers, int timeout, boolean processResponse) throws Exception {
CloseableHttpClient httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build();
DhisHttpResponse dhisHttpResponse;
try {
HttpGet httpGet = new HttpGet(requestURL);
httpGet.setConfig(requestConfig);
if (headers instanceof Map) {
for (Map.Entry<String, String> e : headers.entrySet()) {
httpGet.addHeader(e.getKey(), e.getValue());
}
}
if (authorize) {
httpGet.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
}
HttpResponse response = httpClient.execute(httpGet);
if (processResponse) {
dhisHttpResponse = processResponse(requestURL, username, response);
} else {
dhisHttpResponse = new DhisHttpResponse(response, null, response.getStatusLine().getStatusCode());
}
} catch (Exception e) {
log.error("Exception occurred in the httpGET call with username " + username, e);
throw e;
} finally {
if (httpClient != null) {
httpClient.close();
}
}
return dhisHttpResponse;
}
Aggregations