1.什么是HttpClient?
2.HttpClient特点?
特点:
2.1. 基于标准、纯净的Java语言。实现了Http1.0和Http1.1
2.2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。
2.3. 支持HTTPS协议。
2.4. 通过Http代理建立透明的连接。
2.5. 自动处理Set-Cookie中的Cookie。
3.HttpClient作用?
其主要作用就是通过Http协议,向某个URL地址发起请求,并且获取响应结果。
4.关于httpclient的工具类:
@Service public class ApiService { // 创建Httpclient对象 @Autowired(required=false) private CloseableHttpClient httpclient; /** * 无参的GET请求 * @param url * @return * @throws Exception * @throws IOException */ public String doGet(String url) throws Exception, IOException{ // 创建http GET请求 HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { String data = EntityUtils.toString(response.getEntity(), "UTF-8"); return data; } } finally { if (response != null) { response.close(); } } return null; } // 创建Httpclient对象 模拟浏览器发起访问 打开浏览器 public HttpclientResult doPost(String url) throws ClientProtocolException, IOException{ // 创建http POST请求 输入地址 HttpPost httpPost = new HttpPost(url); httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"); CloseableHttpResponse response = null; try { // 执行请求 敲回车 response = httpclient.execute(httpPost); // 判断返回状态是否为201 Integer code = response.getStatusLine().getStatusCode(); if (code == 201) { String data = EntityUtils.toString(response.getEntity(), "UTF-8"); HttpclientResult result = new HttpclientResult(code, data); return result; } } finally { if (response != null) { response.close(); } } return null; } /** * 有参GET * @param url * @return * @throws Exception * @throws IOException */ public String doGetParam(String url,Map<String,String> params) throws Exception, IOException{ // 定义请求的参数 URIBuilder uriBuilder = new URIBuilder(url); if(params!=null && !params.isEmpty()){ for(String key:params.keySet()){ uriBuilder.setParameter(key, params.get(key)); } } URI uri = uriBuilder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { String data = EntityUtils.toString(response.getEntity(), "UTF-8"); return data; } } finally { if (response != null) { response.close(); } } return null; } /** * 有参post * @param url * @return * @throws ClientProtocolException * @throws IOException */ public HttpclientResult doPostParam(String url,Map<String,String> params) throws ClientProtocolException, IOException{ // 创建http POST请求 输入地址 HttpPost httpPost = new HttpPost(url); if(params!=null&&!params.isEmpty()){ List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); for (String key : params.keySet()) { parameters.add(new BasicNameValuePair(key, params.get(key))); } // 构造一个form表单式的实体 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); // 将请求实体设置到httpPost对象中 httpPost.setEntity(formEntity); } // 模拟浏览器访问 httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"); // 调用接口访问对象 CloseableHttpResponse response = null; try { // 执行请求 敲回车 response = httpclient.execute(httpPost); // 判断返回状态是否为201 Integer code = response.getStatusLine().getStatusCode(); String data = EntityUtils.toString(response.getEntity(), "UTF-8"); HttpclientResult result = new HttpclientResult(code, data); return result; } finally { if (response != null) { response.close(); } } } }