import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; public class HttpClientExample { public static void main(String[] args) { try { HttpClient httpClient = new HttpClient(); httpClient.start(); Request request = httpClient.newRequest("https://api.example.com/api/endpoint") .method(HttpMethod.GET) .header("Content-Type", "application/json") .timeout(5000); ContentResponse response = request.send(); System.out.println(response.getContentAsString()); httpClient.stop(); } catch (Exception e) { e.printStackTrace(); } } } ContentResponse response = request.send(); String content = response.getContentAsString(); int statusCode = response.getStatus(); Map<String, List<String>> headers = response.getHeaders(); import org.eclipse.jetty.client.util.FutureResponseListener; public class AsyncHttpClientExample { public static void main(String[] args) { try { HttpClient httpClient = new HttpClient(); httpClient.start(); Request request = httpClient.newRequest("https://api.example.com/api/endpoint") .method(HttpMethod.GET) .header("Content-Type", "application/json") .timeout(5000); FutureResponseListener futureResponseListener = new FutureResponseListener(request); request.send(futureResponseListener); ContentResponse response = futureResponseListener.get(); System.out.println(response.getContentAsString()); httpClient.stop(); } catch (Exception e) { e.printStackTrace(); } } }


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