获得Http客户端(可以理解为:你得先有一个浏览器;注意:实际上HttpClient与浏览器是不一样的)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
@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 {
HttpPost request = new HttpPost("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:8080/hello");
String response = this.httpClient.execute(request, new BasicResponseHandler());
System.out.println(response);
}
httpClient用法
最新推荐文章于 2024-07-11 15:24:07 发布