# httpclient_easy **Repository Path**: oswl/httpclient_easy ## Basic Information - **Project Name**: httpclient_easy - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-07-06 - **Last Updated**: 2021-07-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # HttpClient的小测试 ```java import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.junit.Before; import org.junit.Test; import java.io.IOException; /** * http客户端工具测试 * * @author wl */ public class HttpClientTest { private CloseableHttpClient httpClient; @Before public void init() { httpClient = HttpClients.createDefault(); } // 测试get请求 @Test public void testGet() throws IOException { HttpGet request = new HttpGet("http://www.baidu.com"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } // 测试post请求 @Test public void testPost() throws Exception { HttpPost request = new HttpPost("https://www.oschina.net/"); request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } // 测试获取对象信息 @Test public void testGetBean() throws IOException { HttpGet httpGet = new HttpGet("http://localhost:8080/user/list"); String response = this.httpClient.execute(httpGet, new BasicResponseHandler()); System.out.println(response); } } ``` # RestTemplate测试 ```java import com.wanglei.HttpClientApplication; import com.wanglei.bean.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.client.RestTemplate; /** * RestTemplate测试 * * @author wl */ @RunWith(SpringRunner.class) @SpringBootTest(classes = HttpClientApplication.class) public class HttpApplicationTest { @Autowired private RestTemplate restTemplate; // 测试get请求 @Test public void httpGet() { // 如何处理获取一个指定对象的集合 User[] users = restTemplate.getForObject("http://localhost:8080/user/list", User[].class); if (users != null) { for (User user : users) { System.out.println(user); } } } } ```