1, look at the sample code
public class HttpClientTest {
public static void main(String args[]) {
//创建HttpClientBuilder
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
//HttpClient
CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet("http://www.gxnu.edu.cn/default.html");
System.out.println(httpGet.getRequestLine());
try {
//执行get请求
HttpResponse httpResponse = closeableHttpClient.execute(httpGet);
//获取响应消息实体
HttpEntity entity = httpResponse.getEntity();
//响应状态
System.out.println("status:" + httpResponse.getStatusLine());
//判断响应实体是否为空
if (entity != null) {
System.out.println("contentEncoding:" + entity.getContentEncoding());
System.out.println("response content:" + EntityUtils.toString(entity));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流并释放资源
closeableHttpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、HttpClientBuilder
HttpClientBuilder used to create CloseableHttpClient instance. I looked at the API documentation, AbstractHttpClient , AutoRetryHttpClient , DefaultHttpClient so is deprecated, use HttpClientBuilder instead.
3、CloseableHttpClient
Implement an interface: of Closeable , AutoCloseable , HttpClient ; subclasses: AbstractHttpClient
4、HttpGet
Non-thread-safe; HttpGet has three constructors: HttpGet (), HttpGet (String uri), HttpGet (URI uri)
5、HttpResponse
HTTP server returns a response message after receiving and interpreting a request
Response = Status-Line
*(( general-header
| response-header
| entity-header ) CRLF)
CRLF
[ message-body ]
Reproduced in: https: //my.oschina.net/tanweijie/blog/195287