HttpClient
pom
1 2 3 4 5
| <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.1</version> </dependency>
|
1 2 3 4 5 6 7 8 9
| public class User implements Serializable { private static final long serialVersionUID = 1L; private Long id; private String userName; private String password; private String name;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @RunWith(SpringRunner.class) @SpringBootTest(classes = HttpDemoApplication.class) public class HttpDemoApplicationTests {
@Autowired private RestTemplate restTemplate;
@Test public void httpGet() { User user = this.restTemplate.getForObject("http://localhost/hello", User.class); System.out.println(user); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class HttpTests { CloseableHttpClient httpClient;
@Before public void init() { httpClient = HttpClients.createDefault(); }
@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); }
@Test public void testPost() throws IOException { HttpGet request = new HttpGet("http://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 testGetPojo() throws IOException { HttpGet request = new HttpGet("http://localhost/hello"); String response = this.httpClient.execute(request, new BasicResponseHandler()); System.out.println(response); } }
|
https://www.jianshu.com/p/375be5929bed


—-
https://www.cnblogs.com/ITtangtang/p/3968093.html
https://www.cnblogs.com/bethunebtj/p/8493379.html
Author:
John Doe
Permalink:
http://yoursite.com/2019/03/15/工具类🔧/HttpClient/
License:
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan:
Do you believe in DESTINY?