import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; public class HttpClientExample { public static void main(String[] args) { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod("http://example.com/api/users"); try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode == HttpStatus.SC_OK) { String responseBody = getMethod.getResponseBodyAsString(); System.out.println("Response: " + responseBody); } else { System.err.println("HTTP Request failed: " + getMethod.getStatusLine()); } } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); } finally { getMethod.releaseConnection(); } } }


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