<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.34</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> ClientConfig config = new ClientConfig(); config.connectorProvider(new ApacheConnectorProvider()); Client client = ClientBuilder.newClient(config); WebTarget target = client.target("http://example.com/resource"); Response response = target.request().get(); if (response.getStatus() == 200) { String result = response.readEntity(String.class); System.out.println(result); } else { System.out.println("Request failed with status: " + response.getStatus()); } response.close(); WebTarget target = client.target("http://example.com/resource"); Entity<String> entity = Entity.entity("request body", MediaType.TEXT_PLAIN); Response response = target.request().post(entity); if (response.getStatus() == 200) { String result = response.readEntity(String.class); System.out.println(result); } else { System.out.println("Request failed with status: " + response.getStatus()); } response.close(); ClientConfig config = new ClientConfig(); config.property(ClientProperties.CONNECT_TIMEOUT, 5000); config.property(ClientProperties.READ_TIMEOUT, 5000); config.property(ClientProperties.PROXY_URI, "http://proxy.example.com:8080"); Client client = ClientBuilder.newClient(config);


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