对httpclient4.3版本的一个简单封装,下面是代码
/** * httputil工具类 * * @author rex */ public class HttpUtil { private static CloseableHttpClient client; private static BasicCookieStore cookieStore; private static HttpGet get; private static HttpPost post; private static HttpResponse response; private static Result result; private static HttpEntity entity; /** * //TODO get请求 * * @param url 请求地址 * @param headers 请求头 * @param params 请求参数 * @param encoding 请求编码 * @return * @throws IOException * @throws ClientProtocolException */ public static Result get(String url, Map<String, String> headers, Map<String, String> params) throws ClientProtocolException, IOException { cookieStore = new BasicCookieStore(); client = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); url = (null == params ? url : url + "?" + parseParam(params)); get = new HttpGet(url); get.setHeaders(parseHeader(headers)); response = client.execute(get); entity = response.getEntity(); result = new Result(); result.setHttpClient(client); result.setCookies(cookieStore.getCookies()); result.setStatusCode(response.getStatusLine().getStatusCode()); result.setHeaders(response.getAllHeaders()); result.setHttpEntity(entity); result.setBody(EntityUtils.toString(entity)); return result; } /** * //TODO post请求 * * @param url 请求地址 * @param headers 请求头 * @param params 请求参数 * @param encoding 请求编码 * @return * @throws IOException * @throws ClientProtocolException */ public static Result post(String url, Map<String, String> headers, Map<String, String> params, String encoding) throws ClientProtocolException, IOException { cookieStore = new BasicCookieStore(); client = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); post = new HttpPost(url); List<NameValuePair> list = new ArrayList<NameValuePair>(); for (String temp : params.keySet()) { list.add(new BasicNameValuePair(temp, params.get(temp))); } post.setEntity(new UrlEncodedFormEntity(list, encoding)); post.setHeaders(parseHeader(headers)); response = client.execute(post); entity = response.getEntity(); result = new Result(); result.setHttpClient(client); result.setCookies(cookieStore.getCookies()); result.setStatusCode(response.getStatusLine().getStatusCode()); result.setHeaders(response.getAllHeaders()); result.setHttpEntity(entity); result.setBody(EntityUtils.toString(entity)); return result; } /** * //TODO 转换header * * @param headers * @return */ private static Header[] parseHeader(Map<String, String> headers) { if (null == headers || headers.isEmpty()) { return getDefaultHeaders(); } Header[] allHeader = new BasicHeader[headers.size()]; int i = 0; for (String str : headers.keySet()) { allHeader[i] = new BasicHeader(str, headers.get(str)); i++; } return allHeader; } /** * //TODO 默认header * * @return */ private static Header[] getDefaultHeaders() { Header[] allHeader = new BasicHeader[2]; allHeader[0] = new BasicHeader("Content-Type", "application/x-www-form-urlencoded"); allHeader[1] = new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"); return allHeader; } /** * //TODO 转换参数列表 * * @param params * @return */ private static String parseParam(Map<String, String> params) { if (null == params || params.isEmpty()) { return ""; } StringBuffer sb = new StringBuffer(); for (String key : params.keySet()) { sb.append(key + "=" + params.get(key) + "&"); } return sb.substring(0, sb.length() - 1); } /** * 释放httpclient对象 */ public static void closeClient(CloseableHttpClient client) { if (null != client) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * 封装请求返回结果 * @author rex * */ public class Result { private CloseableHttpClient httpClient; private List<Cookie> cookies; private HttpEntity httpEntity; private HashMap<String, Header> headerAll; private int statusCode; private String body; public List<Cookie> getCookies() { return cookies; } public void setCookies(List<Cookie> cookies) { this.cookies = cookies; } public CloseableHttpClient getHttpClient() { return httpClient; } public void setHttpClient(CloseableHttpClient httpClient) { this.httpClient = httpClient; } public int getStatusCode() { return statusCode; } public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public void setHeaders(Header[] headers) { headerAll = new HashMap<String, Header>(); for (Header header : headers) { headerAll.put(header.getName(), header); } } public HashMap<String, Header> getHeaderAll() { return headerAll; } public void setHttpEntity(HttpEntity entity) { this.httpEntity = entity; } public HttpEntity getHttpEntity() { return httpEntity; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } }
一个登录小米的测试
public class XiaoMiLogin { public static void main(String[] args) { String login_url = "https://account.xiaomi.com/pass/serviceLoginAuth2"; Map<String, String> params = new HashMap<String, String>(); params.put("user", "小米账号"); params.put("pwd", "密码"); params.put("callback", "https://account.xiaomi.com"); params.put("sid", "passport"); params.put("display", "mobile"); params.put("qs", "%3Fsid%3Dpassport"); params.put("_sign", "KKkRvCpZoDC+gLdeyOsdMhwV0Xg="); try { Result r = HttpUtil.post(login_url, null, params, "UTF-8"); for(Cookie cookie : r.getCookies()){ System.out.println(cookie.getName() + "=" + cookie.getValue()); } HttpUtil.closeClient(r.getHttpClient()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }