package com.gxx.record.utils; /** *
*
Title:
*
* none
*
Description:
*
*

* none

*
* * @author Gxx * @version 1.0, 2014-5-21 * @since sirius-commons * */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.gxx.record.utils.alipay.AlipayRSA; public class HttpClientUtils { private static final Log logger = LogFactory.getLog(HttpClientUtils.class); /** * 获得网页中的所有HTML内容 * * @param url * @param charset * @return */ public static String getWebContentByGet(String url, String charset) { HttpClient client = new HttpClient(); GetMethod getMethod = new GetMethod(url); StringBuilder sb = new StringBuilder(); try { getMethod.setRequestHeader("Connection", "close"); getMethod.getParams().setBooleanParameter( "http.protocol.expect-continue", false); // 状态码 int statusCode = client.executeMethod(getMethod); if (statusCode == HttpStatus.SC_OK) { // 获得HTML文本 BufferedReader bf = new BufferedReader(new InputStreamReader( getMethod.getResponseBodyAsStream(), charset)); String line = null; while ((line = bf.readLine()) != null) { sb.append(line); } bf.close(); } } catch (Exception e) { logger.error(e); } finally { if (getMethod != null) getMethod.releaseConnection(); if (client != null) { client = null; } } return sb.toString(); } public static String getWebContentByGet(String url) { return getWebContentByGet(url, "utf-8"); } /** * 获得网页中的所有HTML内容 * * @param url * @param mapData * :传递的参数 * @param charset * @return */ @SuppressWarnings("rawtypes") public static String getWebContentByPost(String url, Map mapData, String charset) { HttpClient client = new HttpClient(); InputStream is = null; PostMethod postMethod = new PostMethod(url); postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); StringBuilder sb = new StringBuilder(); // 填入各个表单域的值 NameValuePair[] data = new NameValuePair[mapData.size()]; Set set = mapData.entrySet(); Iterator iterator = set.iterator(); int i = 0; while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); data[i] = new NameValuePair((String) entry.getKey(), entry.getValue() == null ? StringUtils.EMPTY : (String) entry.getValue()); i++; } // 将表单的值放入postMethod中 postMethod.setRequestBody(data); BufferedReader bf = null; InputStreamReader isReader = null; try { int statusCode = client.executeMethod(postMethod); if (statusCode == HttpStatus.SC_OK) { // 获得HTML文本 is = postMethod.getResponseBodyAsStream(); isReader = new InputStreamReader(is, charset); bf = new BufferedReader(isReader); String line = null; while ((line = bf.readLine()) != null) { sb.append(line); } bf.close(); } } catch (HttpException e) { logger.error(e); } catch (IOException e) { logger.error(e); } finally { if (is != null) { try { is.close(); } catch (Exception e) { logger.error("关闭流错误", e); } } if (bf != null) { try { bf.close(); } catch (Exception ex) { logger.error("关闭流句柄错误", ex); } } if (isReader != null) { try { isReader.close(); } catch (Exception ex) { logger.error("关闭流句柄错误", ex); } } if (postMethod != null) postMethod.releaseConnection(); if (client != null) { client = null; } } return sb.toString(); } //私钥用于签名 public static final String PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALRLWLgujFrTx5hn" + "/tWVJS8F1cyFwel+kYUls5hnWslEnRq5RoShByiMmLJ2SuhzICxDs8dztDX27nJN" + "zrK+zR7DFC2OWCGGKZCB9sL9fPxQZka80etUGpEa4k/ozNUV0AA7g8cF2sYP402Q" + "ilKLtY3i9n8v4FLmoBrQnlFkev6DAgMBAAECgYEAgV63cKHmEJ56UD//Od7laZJ0" + "UWeBNzycyr+82RUArf9QBAcCNNUfP9dzEkuTgKxrX5fM/sdlfnuLqfVu5g/HOqEK" + "Nf7Du6EHNCoYrk1pO8TDIe+FmGf1YMhFNSXUyy/iiBp8DhIZBPuw8CJDXk/HDfX8" + "4ZVMwyI54XogHIyvVZECQQDfuEfk2pX83j0vPFtvfsn38pMkvuX74bCSMHC1d/vp" + "2qhIEBkMnSWIOCF0xyXZOtyCxr3dQdq0w75lP13dQnr9AkEAzk8FuIGBiY/Ycxek" + "yTaOKR5h7vtMFXBYaIWjhRvFH7uu1BV3WYZRUZ+AcrajzR0gVh8vw/fHMJTXjt3g" + "C8RXfwJAGbjHU1GqcwNeR4MKBDywnfhe4JeuO5KMSZKWMtcTuKRyOq7XInMi8JF7" + "j0CiYmJ5HfQZukmrCqOCzYNGmHzfRQJASvJqIZzqcg7PkwAD3RT0j/t5mFeyTBeE" + "BnSF8tUjx2NnH6SsttkUsZJAF2xCFUF/fcnWkFpzAjMQG9Z+jcfGAQJBAJix01pB" + "q1G/CbmE1kkftR8lOnnU899SxaAkF9pxZ0NfCpU0gQfZ/ez6frFJi59RyFon5sRk" + "xSMfGfCSZtmEytY="; public static void main(String[] str) throws Exception { /** * (一)根据code获取token和userid */ String code="4c8bb8acbe964e2b9c8e2121f61eYD37";//第一步中获取的auth_code //根据code获取token和userid StringBuffer payParams = new StringBuffer(); //生产加密签名明文 payParams.append("app_id").append("=").append("2015012700028257").append("&"); payParams.append("charset").append("=").append("GBK").append("&"); payParams.append("code").append("=").append(code).append("&"); payParams.append("grant_type").append("=").append("authorization_code").append("&"); payParams.append("method").append("=").append("alipay.system.oauth.token").append("&"); //payParams.append("refresh_token").append("=").append("").append("&"); payParams.append("sign_type").append("=").append("RSA").append("&"); payParams.append("timestamp").append("=").append("2016-01-19 08:08:08").append("&"); payParams.append("version").append("=").append("1.0"); String signValue = AlipayRSA.sign(payParams.toString(), PRIVATE_KEY, "GBK"); System.out.println(signValue); //根据code获取token和userid Map map = new HashMap(); map.put("app_id", "2015012700028257"); map.put("charset", "GBK"); map.put("code", code); map.put("grant_type", "authorization_code"); map.put("method", "alipay.system.oauth.token"); //map.put("refresh_token", ""); map.put("sign_type", "RSA"); map.put("timestamp", "2016-01-19 08:08:08"); map.put("version", "1.0"); map.put("sign", signValue); String content = HttpClientUtils.getWebContentByPost( "https://openapi.alipay.com/gateway.do", map, "GBK"); System.out.println(content); /** * (二)根究token获取用户信息 */ // String token = "composeB7fddb71c31e04d4d8e43f500ee383X37";//第二步中获取的access_token // // StringBuffer payParams = new StringBuffer(); // //生产加密签名明文 // payParams.append("app_id").append("=").append("2015012700028257").append("&"); // payParams.append("auth_token").append("=").append(token).append("&"); // payParams.append("charset").append("=").append("GBK").append("&"); // payParams.append("method").append("=").append("alipay.user.userinfo.share").append("&"); // payParams.append("sign_type").append("=").append("RSA").append("&"); // payParams.append("timestamp").append("=").append("2016-01-19 08:08:08").append("&"); // payParams.append("version").append("=").append("1.0"); // String signValue = AlipayRSA.sign(payParams.toString(), PRIVATE_KEY, "GBK"); // System.out.println(signValue); // // //根究token获取用户信息 // Map map = new HashMap(); // map.put("app_id", "2015012700028257"); // map.put("auth_token", token); // map.put("charset", "GBK"); // map.put("method", "alipay.user.userinfo.share"); // map.put("sign_type", "RSA"); // map.put("timestamp", "2016-01-19 08:08:08"); // map.put("version", "1.0"); // map.put("sign", signValue); // String content = HttpClientUtils.getWebContentByPost( // "https://openapi.alipay.com/gateway.do", // map, "GBK"); // System.out.println(content); } }