final SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder() .setRequestTimeoutInMs(Integer.MAX_VALUE) .setUrl(url) .build(); client.post(new ByteArrayBodyGenerator(YOUR_BYTE_ARRAY)).get();
@Override protected InputStream getInputStream() throws XmlRpcException { try { client = builder.setHeaders( headers ).build(); // Check the status of the return Response response = client.post( new ByteArrayBodyGenerator( byteOs.toByteArray() ) ).get(); return response.getResponseBodyAsStream(); } catch ( InterruptedException e ) { throw new XmlRpcClientException( "Interruption during communication: " + e.getMessage(), e ); } catch ( ExecutionException e ) { throw new XmlRpcClientException( "Execution error during communication: " + e.getMessage(), e ); } catch ( IOException e ) { throw new XmlRpcClientException( "I/O error in server communication: " + e.getMessage(), e ); } finally { byteOs.reset(); } }
@Test(groups = { "standalone", "default_provider" }) public void testMultiPartPost() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl() + "/multipart").build(); try { Response response = client.post(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")).get(); String body = response.getResponseBody(); String contentType = response.getHeader("X-Content-Type"); assertTrue(contentType.contains("multipart/form-data")); String boundary = contentType.substring(contentType.lastIndexOf("=") + 1); assertTrue(body.startsWith("--" + boundary)); assertTrue(body.trim().endsWith("--" + boundary + "--")); assertTrue(body.contains("Content-Disposition:")); assertTrue(body.contains("Content-Type: application/test")); assertTrue(body.contains("name=\"baPart")); assertTrue(body.contains("filename=\"fileName")); } finally { client.close(); } } }
@Test(groups = { "standalone", "default_provider" }) public void requestByteArrayOutputStreamBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl(getTargetUrl()).build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void byteArrayOutputStreamBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setPooledConnectionIdleTimeout(100).setMaximumConnectionsTotal(50).setRequestTimeout(5 * 60 * 1000).setUrl(getTargetUrl()).setHeader("Content-Type", "text/html").build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new OutputStreamBodyConsumer(o)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void stringBuilderBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setPooledConnectionIdleTimeout(100).setMaximumConnectionsTotal(50).setRequestTimeout(5 * 60 * 1000).setUrl(getTargetUrl()).setHeader("Content-Type", "text/html").build(); try { StringBuilder s = new StringBuilder(); Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())), new AppendableBodyConsumer(s)); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(s.toString(), MY_MESSAGE); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void inputStreamBodyConsumerTest() throws Throwable { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setPooledConnectionIdleTimeout(100).setMaximumConnectionsTotal(50).setRequestTimeout(5 * 60 * 1000).setUrl(getTargetUrl()).setHeader("Content-Type", "text/html").build(); try { Future<Response> future = client.post(new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes()))); System.out.println("waiting for response"); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(response.getResponseBody(), MY_MESSAGE); } finally { client.close(); } }
@Test(groups = { "standalone", "default_provider" }) public void testDeriveOverrideURL() throws Exception { SimpleAsyncHttpClient client = new SimpleAsyncHttpClient.Builder().setProviderClass(getProviderClass()).setUrl("http://invalid.url").build(); SimpleAsyncHttpClient derived = client.derive().setUrl(getTargetUrl()).build(); try { ByteArrayOutputStream o = new ByteArrayOutputStream(10); InputStreamBodyGenerator generator = new InputStreamBodyGenerator(new ByteArrayInputStream(MY_MESSAGE.getBytes())); OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o); Future<Response> future = derived.post(generator, consumer); Response response = future.get(); assertEquals(response.getStatusCode(), 200); assertEquals(o.toString(), MY_MESSAGE); } finally { client.close(); derived.close(); } }
OutputStreamBodyConsumer consumer = new OutputStreamBodyConsumer(o); Future<Response> future = client.post(generator, consumer);