import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = HttpClientBuilder.create().build(); try { HttpResponse response = httpClient.execute(request); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("Status Code: " + statusCode); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line; StringBuilder responseContent = new StringBuilder(); while ((line = reader.readLine()) != null) { responseContent.append(line); } System.out.println("Response Body: " + responseContent.toString()); } catch (IOException e) { e.printStackTrace(); } } } import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; public class OkHttpExample { public static void main(String[] args) { OkHttpClient httpClient = new OkHttpClient(); Request request = new Request.Builder() .build(); try { Response response = httpClient.newCall(request).execute(); int statusCode = response.code(); System.out.println("Status Code: " + statusCode); String responseBody = response.body().string(); System.out.println("Response Body: " + responseBody); } catch (IOException e) { e.printStackTrace(); } } }


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