@Override public void run() { final UsageStatisticsReportDTO usageStatsReport = generateReport(); final String usageStatsReportJson = usageStatsReport.toJson(); final HttpClientWrapper clientWrapper = HttpClientWrapper.create() .setConnectionTimeout(30 * 1000) .setSocketTimeout(30 * 1000); if(m_useSystemProxy) { clientWrapper.useSystemProxySettings(); } try (CloseableHttpClient client = clientWrapper.getClient()) { HttpPost httpRequest = new HttpPost(m_url + USAGE_REPORT); httpRequest.setEntity(new StringEntity(usageStatsReportJson, ContentType.create("application/json", StandardCharsets.UTF_8))); LOG.info("Sending usage statistics report to {}: {}", httpRequest.getURI(), usageStatsReportJson); client.execute(httpRequest); LOG.info("Succesfully sent usage statistics report."); } catch (IOException e) { LOG.info("The usage statistics report was not succesfully delivered: {}", e.getMessage()); } } }
@Override public void run() { final UsageStatisticsReportDTO usageStatsReport = generateReport(); final String usageStatsReportJson = usageStatsReport.toJson(); final HttpClientWrapper clientWrapper = HttpClientWrapper.create() .setConnectionTimeout(30 * 1000) .setSocketTimeout(30 * 1000); if(m_useSystemProxy) { clientWrapper.useSystemProxySettings(); } try (CloseableHttpClient client = clientWrapper.getClient()) { HttpPost httpRequest = new HttpPost(m_url + USAGE_REPORT); httpRequest.setEntity(new StringEntity(usageStatsReportJson, ContentType.create("application/json", StandardCharsets.UTF_8))); LOG.info("Sending usage statistics report to {}: {}", httpRequest.getURI(), usageStatsReportJson); client.execute(httpRequest); LOG.info("Succesfully sent usage statistics report."); } catch (IOException e) { LOG.info("The usage statistics report was not succesfully delivered: {}", e.getMessage()); } } }
int statusCode = -1; try { CloseableHttpResponse response = clientWrapper.getClient().execute(postMethod); statusCode = response.getStatusLine().getStatusCode(); contents = EntityUtils.toString(response.getEntity());
/** * Execute the given HTTP method, returning an HTTP response. * * Note that when you are done with the response, you must call {@link #closeResponse()} so that it gets cleaned up properly. */ public CloseableHttpResponse execute(final HttpUriRequest method) throws ClientProtocolException, IOException { LOG.debug("execute: " + this.toString() + "; method: " + method.toString()); // override some headers with our versions final HttpRequestWrapper requestWrapper = HttpRequestWrapper.wrap(method); if (!isEmpty(m_userAgent)) { requestWrapper.setHeader(HTTP.USER_AGENT, m_userAgent); } if (!isEmpty(m_virtualHost)) { requestWrapper.setHeader(HTTP.TARGET_HOST, m_virtualHost); } if (m_version != null) { if (HttpVersion.HTTP_1_1.equals(m_version) && isEmpty(m_virtualHost)) { // NMS-7506, set HTTP version to 1.0 if virtual host is not set (since 1.1 requires a virtual host) requestWrapper.setProtocolVersion(HttpVersion.HTTP_1_0); } else { requestWrapper.setProtocolVersion(m_version); } } return getClient().execute(requestWrapper); }