package org.andy.util; import javax.servlet.http.HttpServletRequest; public class SessionUtil { public static void addAtt(HttpServletRequest request, String key, Object value){ request.getSession().setAttribute(key, value); } public static void removeAtt(HttpServletRequest request, String key){ request.getSession().removeAttribute(key); } public static String getAtt(HttpServletRequest request, String key){ return (String)request.getSession().getAttribute(key); } public static Object getAttObj(HttpServletRequest request, String key){ return request.getSession().getAttribute(key); } public static String optAtt(HttpServletRequest request, String key, String value){ String r = (String)request.getSession().getAttribute(key); if (r == null){ r = value; } return r; } }
HttpKit 网络请求工具类
/** * https 请求 微信为https的请求 * * @author andy * @date 2015-10-9 下午2:40:19 */ public class HttpKit { private static final String DEFAULT_CHARSET = "UTF-8"; /** * @return 返回类型: * @throws IOException * @throws UnsupportedEncodingException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @description 功能描述: get 请求 */ public static String get(String url, Map<String, String> params, Map<String, String> headers) throws IOException, ExecutionException, InterruptedException { AsyncHttpClient http = new AsyncHttpClient(); AsyncHttpClient.BoundRequestBuilder builder = http.prepareGet(url); builder.setBodyEncoding(DEFAULT_CHARSET); if (params != null && !params.isEmpty()) { Set<String> keys = params.keySet(); for (String key : keys) { builder.addQueryParameter(key, params.get(key)); } } if (headers != null && !headers.isEmpty()) { Set<String> keys = headers.keySet(); for (String key : keys) { builder.addHeader(key, params.get(key)); } } Future<Response> f = builder.execute(); String body = f.get().getResponseBody(DEFAULT_CHARSET); http.close(); return body; } /** * @return 返回类型: * @throws IOException * @throws UnsupportedEncodingException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @description 功能描述: get 请求 */ public static String get(String url) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException, IOException, ExecutionException, InterruptedException { return get(url, null); } /** * @return 返回类型: * @throws IOException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @throws UnsupportedEncodingException * @description 功能描述: get 请求 */ public static String get(String url, Map<String, String> params) throws KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, UnsupportedEncodingException, IOException, ExecutionException, InterruptedException { return get(url, params, null); } /** * @return 返回类型: * @throws IOException * @throws NoSuchProviderException * @throws NoSuchAlgorithmException * @throws KeyManagementException * @description 功能描述: POST 请求 */ public static String post(String url, Map<String, String> params) throws IOException, ExecutionException, InterruptedException { AsyncHttpClient http = new AsyncHttpClient(); AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url); builder.setBodyEncoding(DEFAULT_CHARSET); if (params != null && !params.isEmpty()) { Set<String> keys = params.keySet(); for (String key : keys) { builder.addParameter(key, params.get(key)); } } Future<Response> f = builder.execute(); String body = f.get().getResponseBody(DEFAULT_CHARSET); http.close(); return body; } public static String post(String url, String s) throws IOException, ExecutionException, InterruptedException { AsyncHttpClient http = new AsyncHttpClient(); AsyncHttpClient.BoundRequestBuilder builder = http.preparePost(url); builder.setBodyEncoding(DEFAULT_CHARSET); builder.setBody(s); Future<Response> f = builder.execute(); String body = f.get().getResponseBody(DEFAULT_CHARSET); http.close(); return body; } }
支付工具类pay.java