use of org.apache.http.impl.client.CloseableHttpClient in project selenium-tests by Wikia.
the class GraphApi method createTestUser.
private HttpResponse createTestUser(String appId) throws IOException, URISyntaxException {
URL url = new URL(getURLcreateUser(appId));
CloseableHttpClient httpClient = HttpClientBuilder.create().disableAutomaticRetries().build();
HttpPost httpPost = getHttpPost(url);
if (getParams() != null) {
httpPost.setEntity(new UrlEncodedFormEntity(getParams(), StandardCharsets.UTF_8));
}
return httpClient.execute(httpPost);
}
use of org.apache.http.impl.client.CloseableHttpClient in project selenium-tests by Wikia.
the class ApiCall method call.
public void call() {
try {
URL url = new URL(getURL());
CloseableHttpClient httpClient = HttpClientBuilder.create().disableAutomaticRetries().build();
HttpPost httpPost = getHtppPost(url);
// set header
if (getUser() != null) {
httpPost.addHeader("X-Wikia-AccessToken", Helios.getAccessToken(getUser()));
}
// set query params
if (getParams() != null) {
httpPost.setEntity(new UrlEncodedFormEntity(getParams(), StandardCharsets.UTF_8));
}
httpClient.execute(httpPost);
PageObjectLogging.log("CONTENT PUSH", "Content posted to: " + getURL(), true);
} catch (ClientProtocolException e) {
PageObjectLogging.log("EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
} catch (IOException e) {
PageObjectLogging.log("IO EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
} catch (URISyntaxException e) {
PageObjectLogging.log("URI_SYNTAX EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project selenium-tests by Wikia.
the class CommonUtils method sendPost.
public static String sendPost(String apiUrl, String[][] param) {
try {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(apiUrl);
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
for (int i = 0; i < param.length; i++) {
paramPairs.add(new BasicNameValuePair(param[i][0], param[i][1]));
}
httpPost.setEntity(new UrlEncodedFormEntity(paramPairs));
HttpResponse response = null;
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity);
} catch (UnsupportedEncodingException e) {
PageObjectLogging.log("sendPost", e, false);
return null;
} catch (ClientProtocolException e) {
PageObjectLogging.log("sendPost", e, false);
return null;
} catch (IOException e) {
PageObjectLogging.log("sendPost", e, false);
return null;
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project voltdb by VoltDB.
the class TestJSONOverHttps method callProcOverJSON.
private String callProcOverJSON(String varString, final int expectedCode) throws Exception {
URI uri = URI.create("https://localhost:" + m_port + "/api/1.0/");
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sf).build();
// allows multi-threaded use
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
HttpClientBuilder b = HttpClientBuilder.create();
b.setSslcontext(sslContext);
b.setConnectionManager(connMgr);
try (CloseableHttpClient httpclient = b.build()) {
HttpPost post = new HttpPost(uri);
// play nice by using HTTP 1.1 continue requests where the client sends the request headers first
// to the server to see if the server is willing to accept it. This allows us to test large requests
// without incurring server socket connection terminations
RequestConfig rc = RequestConfig.copy(RequestConfig.DEFAULT).setExpectContinueEnabled(true).build();
post.setProtocolVersion(HttpVersion.HTTP_1_1);
post.setConfig(rc);
post.setEntity(new StringEntity(varString, utf8ApplicationFormUrlEncoded));
ResponseHandler<String> rh = new ResponseHandler<String>() {
@Override
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
assertEquals(expectedCode, status);
if ((status >= 200 && status < 300) || status == 400) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
}
return null;
}
};
return httpclient.execute(post, rh);
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project orientdb by orientechnologies.
the class BaseHttpTest method exec.
protected BaseHttpTest exec() throws IOException {
final HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials(getUserName(), getUserPassword()));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
if (keepAlive != null)
request.addHeader("Connection", keepAlive ? "Keep-Alive" : "Close");
if (payload != null && request instanceof HttpEntityEnclosingRequestBase)
((HttpEntityEnclosingRequestBase) request).setEntity(payload);
final CloseableHttpClient httpClient = HttpClients.createDefault();
// DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(retry, false);
// context.setAttribute(HttpMethodParams.RETRY_HANDLER, retryhandler);
response = httpClient.execute(targetHost, request, context);
return this;
}
Aggregations