微信支付发送HTTPS请求的并且解析返回的结果
2017-12-08 18:12
267 查看
一、统一下单支付接口主方法
二、发送HTTPS请求的方法
四、开发过程中用到的jar包
dom-2.0.2.jar
httpmime-4.2.1.jar
httpcore-4.2.1.jar
httpclient-cache-4.2.1.jar
httpclient-4.2.1.jar
commons-lang3.jar
另外这里面还涉及到对传递的参数进行ASII码排序,请参考我的上一篇博客
Map<String,String> params = Maps.newHashMap(); //微信开放平台审核通过的应用APPID params.put("appid", ConstantApp.WEIXIN_APPID); //微信支付分配的商户号 params.put("mch_id", ConstantApp.WEIXIN_MERCHANT_NO); //随机字符串 params.put("nonce_str", RandomStringUtils.random(32, true, true)); //商品描述 params.put("body", subject); //商户订单号 params.put("out_trade_no", outTradeNo); //总金额 params.put("total_fee", totalAmount+""); //终端IP params.put("spbill_create_ip", spbill_create_ip); //通知地址 params.put("notify_url", ConstantApp.WEIXIN_NOTIFY_URL); //交易类型 params.put("trade_type", "APP"); //签名 params.put("sign", CommonService.sign(params)); String xmlStr = HttpsClientUtil.httpsRequest(ConstantApp.WEIXIN_UNIFED_ORDER_URL, "POST", CommonService.getRequestXml(params)); Map<String, String> resultMap = CommonService.doXMLParse(xmlStr);
二、发送HTTPS请求的方法
package common; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.commons.lang3.RandomStringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import ggframework.bottom.log.GGLogger; /** * * 利用HttpClient进行post请求的工具类 * * @version 1.0 * @since JDK1.7 */ public class HttpsClientUtil { // 请求方法 public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) { try { URL url = new URL(requestUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); // 设置请求方式(GET/POST) conn.setRequestMethod(requestMethod); conn.setRequestProperty("content-type", "application/x-www-form-urlencoded"); // 当outputStr不为null时向输出流写数据 if (null != outputStr) { OutputStream outputStream = conn.getOutputStream(); // 注意编码格式 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = conn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } // 释放资源 bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; conn.disconnect(); return buffer.toString(); } catch (ConnectException ce) { GGLogger.error("连接超时:{}" + ce); } catch (Exception e) { GGLogger.info("https请求异常:{}" + e); } return null; } }三、解析微信支付返回的XML格式数据
/** * * 方法描述 解析xml * * @param strxml * @return * * @author yaomy * @date 2017年12月8日 上午11:26:47 */ public static Map<String, String> doXMLParse(String strxml) { Map<String, String> m = new HashMap<String, String>(); try { strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\""); if (null == strxml || "".equals(strxml)) { return null; } InputStream in = new ByteArrayInputStream(strxml.getBytes("UTF-8")); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(in); Element root = doc.getRootElement(); List list = root.getChildren(); Iterator it = list.iterator(); while (it.hasNext()) { Element e = (Element) it.next(); String k = e.getName(); String v = ""; List children = e.getChildren(); if (children.isEmpty()) { v = e.getTextNormaliz c2bc e(); } else { v = getChildrenText(children); } m.put(k, v); } // 关闭流 in.close(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { } catch (JDOMException e) { } return m; } public static String getChildrenText(List children) { StringBuffer sb = new StringBuffer(); if (!children.isEmpty()) { Iterator it = children.iterator(); while (it.hasNext()) { Element e = (Element) it.next(); String name = e.getName(); String value = e.getTextNormalize(); List list = e.getChildren(); sb.append("<" + name + ">"); if (!list.isEmpty()) { sb.append(getChildrenText(list)); } sb.append(value); sb.append("</" + name + ">"); } } return sb.toString(); }
四、开发过程中用到的jar包
dom-2.0.2.jar
httpmime-4.2.1.jar
httpcore-4.2.1.jar
httpclient-cache-4.2.1.jar
httpclient-4.2.1.jar
commons-lang3.jar
另外这里面还涉及到对传递的参数进行ASII码排序,请参考我的上一篇博客
相关文章推荐
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果
- 微信支付发送HTTPS请求的并且解析返回的结果