HttpClient接口协议

httpclient使用步骤

1.导入包
2.创建http请求池
CloseableHttpClient httpClient = HttpClients.createDefault();
3.创建某个请求 post get
HttpGet get = new HttpGet("http://www.baidu.com");

数据封装问题

StringEntity stringEntity = new StringEntity(params, "utf-8");
stringEntity.setContentEncoding("UTF-8");   stringEntity.setContentType("application/json");   
httpPost.setEntity(stringEntity);
4.调用第三步创建好的实例 execute
CloseableHttpResponse response = httpClient.execute(httpPost);
5.处理response 返回HttpEntity
HttpEntity httpEntity = response.getEntity();
6.释放连接 无论执行方法是否成功 都必须释放连接 finally
response.close();

你可能感兴趣的:(杂文)