use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class Usage method testRequestWithExplicitConnectionControl.
@Test
public void testRequestWithExplicitConnectionControl() throws Exception {
HttpClient client = new HttpClient();
client.start();
// Create an explicit connection, and use try-with-resources to manage it
FuturePromise<Connection> futureConnection = new FuturePromise<>();
client.getDestination("http", "localhost", 8080).newConnection(futureConnection);
try (Connection connection = futureConnection.get(5, TimeUnit.SECONDS)) {
Request request = client.newRequest("localhost", 8080);
// Asynchronous send but using FutureResponseListener
FutureResponseListener listener = new FutureResponseListener(request);
connection.send(request, listener);
// Wait for the response on the listener
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
}
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class Usage method testFileUpload.
@Test
public void testFileUpload() throws Exception {
HttpClient client = new HttpClient();
client.start();
// One liner to upload files
Response response = client.newRequest("localhost", 8080).file(Paths.get("file_to_upload.txt")).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class Usage method testGETBlocking.
@Test
public void testGETBlocking() throws Exception {
HttpClient client = new HttpClient();
client.start();
// Address must be provided, it's the only thing non defaultable
Request request = client.newRequest("localhost", 8080).scheme("https").method(HttpMethod.GET).path("/uri").version(HttpVersion.HTTP_1_1).param("a", "b").header("X-Header", "Y-value").agent("Jetty HTTP Client").idleTimeout(5000, TimeUnit.MILLISECONDS).timeout(20, TimeUnit.SECONDS);
ContentResponse response = request.send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class Usage method testRequestInputStream.
@Test
public void testRequestInputStream() throws Exception {
HttpClient client = new HttpClient();
client.start();
InputStream input = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8));
ContentResponse response = client.newRequest("localhost", 8080).content(new InputStreamContentProvider(input)).send();
Assert.assertEquals(200, response.getStatus());
}
use of org.eclipse.jetty.client.HttpClient in project jetty.project by eclipse.
the class Usage method testGETBlocking_ShortAPI.
@Test
public void testGETBlocking_ShortAPI() throws Exception {
HttpClient client = new HttpClient();
client.start();
// Block to get the response
ContentResponse response = client.GET("http://localhost:8080/foo");
// Verify response status code
Assert.assertEquals(200, response.getStatus());
// Access headers
response.getHeaders().get("Content-Length");
}
Aggregations