<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://example.com/api/data"); method.setRequestHeader("Content-Type", "application/json"); method.addParameter("param1", "value1"); method.addParameter("param2", "value2"); int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { InputStream responseBody = method.getResponseBodyAsStream(); } else { } method.releaseConnection(); import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import java.io.InputStream; public class HttpClientExample { public static void main(String[] args) { HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://example.com/api/data"); method.setRequestHeader("Content-Type", "application/json"); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { InputStream responseBody = method.getResponseBodyAsStream(); // ... } else { // ... } } catch (Exception e) { e.printStackTrace(); } finally { method.releaseConnection(); } } }


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