<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.4.30.v20200611</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-client</artifactId> <version>9.4.30.v20200611</version> </dependency> import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.util.IO; import org.junit.jupiter.api.Test; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; public class JettyClientTest { @Test void testHttpGet() throws IOException { HttpClient httpClient = new HttpClient(); ContentResponse response = httpClient.newRequest("http://localhost:8080/api/data") .method(HttpMethod.GET) .send(); assertEquals(HttpStatus.OK_200, response.getStatus()); String responseContent = IO.toString(response.getContent()); System.out.println(responseContent); httpClient.close(); } } import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.http.HttpMethod; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.util.IO; import org.junit.jupiter.api.Test; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; public class JettyClientTest { @Test void testHttpPost() throws IOException { HttpClient httpClient = new HttpClient(); ContentResponse response = httpClient.newRequest("http://localhost:8080/api/data") .method(HttpMethod.POST) .content("field1=value1&field2=value2") .send(); assertEquals(HttpStatus.OK_200, response.getStatus()); String responseContent = IO.toString(response.getContent()); System.out.println(responseContent); httpClient.close(); } }


上一篇:
下一篇:
切换中文