import java.net.URI; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(new URI("http://example.com/api/resource?param1=value1&param2=value2")); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } } } import java.net.URI; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(new URI("http://example.com/api/resource")); StringEntity requestBody = new StringEntity("param1=value1&param2=value2"); httpPost.setEntity(requestBody); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } } } import java.net.URI; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPut httpPut = new HttpPut(new URI("http://example.com/api/resource")); StringEntity requestBody = new StringEntity("param1=new_value1&param2=new_value2"); httpPut.setEntity(requestBody); try (CloseableHttpResponse response = httpClient.execute(httpPut)) { String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } } } import java.net.URI; import org.apache.http.client.methods.HttpDelete; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class HttpClientExample { public static void main(String[] args) throws Exception { CloseableHttpClient httpClient = HttpClients.createDefault(); HttpDelete httpDelete = new HttpDelete(new URI("http://example.com/api/resource")); try (CloseableHttpResponse response = httpClient.execute(httpDelete)) { String responseBody = EntityUtils.toString(response.getEntity()); System.out.println(responseBody); } } }


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