use of org.apache.http.HttpResponse 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.HttpResponse 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.HttpResponse in project hadoop-pcap by RIPE-NCC.
the class HttpPcapReader method processPacketPayload.
@Override
protected void processPacketPayload(Packet packet, final byte[] payload) {
String protocol = (String) packet.get(Packet.PROTOCOL);
if (!PcapReader.PROTOCOL_TCP.equals(protocol))
return;
HttpPacket httpPacket = (HttpPacket) packet;
Integer srcPort = (Integer) packet.get(Packet.SRC_PORT);
Integer dstPort = (Integer) packet.get(Packet.DST_PORT);
if ((HTTP_PORT == srcPort || HTTP_PORT == dstPort) && packet.containsKey(Packet.REASSEMBLED_TCP_FRAGMENTS)) {
final SessionInputBuffer inBuf = new AbstractSessionInputBuffer() {
{
init(new ByteArrayInputStream(payload), 1024, params);
}
@Override
public boolean isDataAvailable(int timeout) throws IOException {
return true;
}
};
final SessionOutputBuffer outBuf = new AbstractSessionOutputBuffer() {
};
if (HTTP_PORT == srcPort) {
HttpMessageParser<HttpResponse> parser = new DefaultHttpResponseParser(inBuf, null, respFactory, params);
HttpClientConnection conn = new DefaultClientConnection() {
{
init(inBuf, outBuf, params);
}
@Override
protected void assertNotOpen() {
}
@Override
protected void assertOpen() {
}
};
try {
HttpResponse response = parser.parse();
conn.receiveResponseEntity(response);
propagateHeaders(httpPacket, response.getAllHeaders());
} catch (IOException e) {
LOG.error("IOException when decoding HTTP response", e);
} catch (HttpException e) {
LOG.error("HttpException when decoding HTTP response", e);
}
} else if (HTTP_PORT == dstPort) {
HttpMessageParser<HttpRequest> parser = new DefaultHttpRequestParser(inBuf, null, reqFactory, params);
try {
HttpRequest request = parser.parse();
propagateHeaders(httpPacket, request.getAllHeaders());
} catch (IOException e) {
LOG.error("IOException when decoding HTTP request", e);
} catch (HttpException e) {
LOG.error("HttpException when decoding HTTP request", e);
}
}
}
}
use of org.apache.http.HttpResponse in project java-chassis by ServiceComb.
the class HttpsClient method get.
private static HttpResponse get(HttpClient httpClient, String url, Map<String, String> headers) throws ClientProtocolException, IOException {
HttpGet getRequest = new HttpGet(url);
for (Entry<String, String> header : headers.entrySet()) {
getRequest.addHeader(header.getKey(), header.getValue());
}
HttpResponse response = httpClient.execute(getRequest);
return response;
}
use of org.apache.http.HttpResponse in project java-chassis by ServiceComb.
the class TestHttpsClient method testHttpClient.
@Test
public void testHttpClient() throws ClientProtocolException, IOException {
// test valid Invalid Inputs
Map<String, String> oHeaders = new HashMap<String, String>();
oHeaders.put("X-Auth", "JHGUGJGH");
HttpResponse oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("", oHeaders, "//check", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("UNKNOWN METHOD", null, "//check", "body", null);
Assert.assertEquals(null, oResponse);
// With default Bean
HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
oBean.setKeyStorePath("JHGJ");
oBean.setKeyStorePasswd("HJGJH");
oBean.setTrustStorePasswd("JHGJHG");
oBean.setTrustStorePath("JHGJGj");
Assert.assertEquals("JHGJGj", oBean.getTrustStorePath());
Assert.assertEquals("JHGJHG", oBean.getTrustStorePasswd());
oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", oBean);
Assert.assertEquals(null, oResponse);
HttpsClient.getHttpsClient(oBean);
Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
//Handle Error Scenarios
try {
oResponse = HttpsClient.execute("POST", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
try {
oResponse = HttpsClient.execute("GET", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
try {
oResponse = HttpsClient.execute("DELETE", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
// TODO Check the valid Responses
}
Aggregations