import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; public class HttpClientExample { public static void main(String[] args) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod("https://api.example.com/data"); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { String responseBody = method.getResponseBodyAsString(); System.out.println("Response: " + responseBody); } else { System.out.println("Request failed: " + method.getStatusLine()); } } catch (Exception e) { e.printStackTrace(); } finally { method.releaseConnection(); } } }


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