您的位置:首页 > 移动开发 > 微信开发

微信现金红包开发(java)

2016-01-04 16:46 585 查看
最近在开发微信红包,和网友分享下。主要有两个注意点:

1. 请求发送微信红包的请求为ssl请求, 因此不能用普通的post请求开发。

4000
2. 生成签名需要将所传的参数全部提交用来生成签名, 否则签名失败, 当然需要主要大小写, 以及Assic排序,  因为生成的签名是MD5加密的。

贴上关键代码

@Override
public String createSign(WxCashRedJson wxCashRed) {
SortedMap<String, String> sort = new TreeMap<String, String>();
sort.put("act_name", wxCashRed.getAct_name());
sort.put("client_ip", wxCashRed.getClient_ip());
sort.put("mch_billno",wxCashRed.getMch_billno());
sort.put("mch_id", wxCashRed.getMch_id());
sort.put("nonce_str", wxCashRed.getNonce_str());
sort.put("re_openid", wxCashRed.getRe_openid());
sort.put("remark", wxCashRed.getRemark());
sort.put("send_name", wxCashRed.getSend_name());
sort.put("total_amount", String.valueOf(wxCashRed.getTotal_amount()));
sort.put("total_num", String.valueOf(wxCashRed.getTotal_num()));
sort.put("wishing", wxCashRed.getWishing());
sort.put("wxappid", wxCashRed.getWxappid());
return createSign(sort);
}

public String createSign(SortedMap<String, String> packageParams) {
StringBuffer sb = new StringBuffer();
@SuppressWarnings("rawtypes")
Set es = packageParams.entrySet();
@SuppressWarnings("rawtypes")
Iterator it = es.iterator();
while (it.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k)
&& !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + ConstantUtils.CORP_KEY);
System.out.println("md5 sb:" + sb);
logger.info("md5 sb:" + sb);
String sign = MD5Util.MD5Encode(sb.toString(), "utf-8")
.toUpperCase();
System.out.println("sign签名:" + sign);
logger.info("sign签名:" + sign);
return sign;

}

public static <T> T requestReturnXmlWithSSL(WxAPI api, Class<T> clazz, String mchId) throws Exception {
api.buildRequest();

// load PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
InputStream stream = AbstractWxAPI.class.getResourceAsStream("apiclient_cert.p12.bak.p12");

try {
keyStore.load(stream, mchId.toCharArray());
} finally {
stream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build();

// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());

StringEntity entity = new StringEntity(body, ContentType.create("application/json", Consts.UTF_8));
System.out.println("body: "+body);
entity.setChunked(true);
HttpPost httppost = new HttpPost(url);
httppost.setEntity(entity);

CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

CloseableHttpResponse response = httpclient.execute(httppost);
StringBuffer sb = new StringBuffer();
try {
HttpEntity responseEntity = response.getEntity();

if (responseEntity != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseEntity.getContent()));
String text;
while ((text = bufferedReader.readLine()) != null) {
sb.append(text);
}
System.out.println(sb);
}
} catch (Exception e) {
response.close();
e.printStackTrace();
}

T responseValue = XmlUtils.stringToObject(clazz, sb.toString());
return responseValue;
}

有什么问题, 各位请留言或者加我微信 wangyan199366
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息