微信支付

java 微信支付

    <div class="article_manage clearfix">
    <div class="article_l">
        <span class="link_categories">
        标签:
          <a href="http://www.csdn.net/tag/%e5%be%ae%e4%bf%a1" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">微信</a><a href="http://www.csdn.net/tag/%e6%94%af%e4%bb%98" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">支付</a><a href="http://www.csdn.net/tag/%e5%85%ac%e4%bc%97%e5%8f%b7%e6%94%af%e4%bb%98" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">公众号支付</a>
        </span>
    </div>
    <div class="article_r">
        <span class="link_postdate">2017-04-05 15:31</span>
        <span class="link_view" title="阅读次数">4867人阅读</span>
        <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
        <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('java+%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98','69258069');return false;" title="收藏" target="_blank">收藏</a></span>
         <span class="link_report"> <a href="#report" onclick="javascript:report(69258069,2);return false;" title="举报">举报</a></span>

    </div>
</div>    <style type="text/css">        
        .embody{
            padding:10px 10px 10px;
            margin:0 -20px;
            border-bottom:solid 1px #ededed;                
        }
        .embody_b{
            margin:0 ;
            padding:10px 0;
        }
        .embody .embody_t,.embody .embody_c{
            display: inline-block;
            margin-right:10px;
        }
        .embody_t{
            font-size: 12px;
            color:#999;
        }
        .embody_c{
            font-size: 12px;
        }
        .embody_c img,.embody_c em{
            display: inline-block;
            vertical-align: middle;               
        }
         .embody_c img{               
            width:30px;
            height:30px;
        }
        .embody_c em{
            margin: 0 20px 0 10px;
            color:#333;
            font-style: normal;
        }
</style>
<script type="text/javascript">
    $(function () {
        try
        {
            var lib = eval("("+$("#lib").attr("value")+")");
            var html = "";
            if (lib.err == 0) {
                $.each(lib.data, function (i) {
                    var obj = lib.data[i];
                    //html += '<img src="' + obj.logo + '"/>' + obj.name + "&nbsp;&nbsp;";
                    html += ' <a href="' + obj.url + '" target="_blank">';
                    html += ' <img src="' + obj.logo + '">';
                    html += ' <em><b>' + obj.name + '</b></em>';
                    html += ' </a>';
                });
                if (html != "") {
                    setTimeout(function () {
                        $("#lib").html(html);                      
                        $("#embody").show();
                    }, 100);
                }
            }      
        } catch (err)
        { }

    });
</script>
  <div class="category clearfix">
    <div class="category_l">
       <img src="http://static.blog.csdn.net/images/category_icon.jpg">
        <span>分类:</span>
    </div>
    <div class="category_r">
                <label onclick="GetCategoryArticles('2824663','u011072139','top','69258069');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">java<em>(62)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/u011072139/article/category/2824663" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_2824663">                            
                        </ul>
                    </div>
                </label>                    
                <label onclick="GetCategoryArticles('2824665','u011072139','top','69258069');">
                    <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">微信<em>(19)</em></span>
                  <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
                  <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
                    <div class="subItem">
                        <div class="subItem_t"><a href="http://blog.csdn.net/u011072139/article/category/2824665" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
                        <ul class="subItem_l" id="top_2824665">                            
                        </ul>
                    </div>
                </label>                    
    </div>
</div>
  1. /** 
  2.  * 统一下单工具类 
  3.  * @author why 
  4.  * 
  5.  */  
  6. public class TyxdUtil {  
  7.     /** 
  8.      * json数据 
  9.      * @param tyxd 
  10.      * @return 
  11.      */  
  12.     public static String makeTyxdMessage(Tyxd tyxd){  
  13.         SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();  
  14.         parameters.put(”appid”, tyxd.getAppid());  
  15.         parameters.put(”mch_id”, tyxd.getMch_id());  
  16.         parameters.put(”nonce_str”, tyxd.getNonce_str());  
  17.         parameters.put(”body”, tyxd.getBody());  
  18.         parameters.put(”out_trade_no”, tyxd.getOut_trade_no());  
  19.         parameters.put(”total_fee”, tyxd.getTotal_fee());  
  20.         parameters.put(”spbill_create_ip”,tyxd.getSpbill_create_ip());  
  21.         parameters.put(”notify_url”, tyxd.getNotify_url());  
  22.         parameters.put(”trade_type”, tyxd.getTrade_type());  
  23.         parameters.put(”openid”, tyxd.getOpenid());  
  24.         String sign = PayCommonUtil.createSign(”UTF-8”, parameters);  
  25.         parameters.put(”sign”, sign);  
  26.         String requestXML = PayCommonUtil.getRequestXml(parameters);  
  27.         return requestXML;  
  28.     }  
  29.     /** 
  30.      * 调用统一下单接口 
  31.      * @param requestXML 
  32.      * @return 
  33.      * @throws Exception 
  34.      */  
  35.     public static Map<String, String> sendTyxdMessage(String requestXML) throws Exception{  
  36.         String result =CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, ”POST”, requestXML);  
  37.         Map<String, String> map = XMLUtil.doXMLParse(result);//解析微信返回的信息,以Map形式存储便于取值  
  38.         return map;  
  39.     }  
  40.     public static void main(String[] args) throws Exception {  
  41.         Tyxd t = new Tyxd();  
  42.         t.setAppid(ConfigUtil.APPID);//公众号id  
  43.         t.setMch_id(ConfigUtil.MCH_ID);//商户号  
  44.         t.setNonce_str(PayCommonUtil.CreateNoncestr());//随机字符串  
  45. //      t.setSign(sign);  
  46.         t.setBody(”支付测试”);//商品描述  
  47.         t.setOut_trade_no(”1441332471849”);//订单号  
  48.         t.setTotal_fee(”1”);//总金额  
  49.         t.setSpbill_create_ip(”127.0.0.1”);//终端ip  
  50.         t.setNotify_url(ConfigUtil.NOTIFY_URL);//通知地址  
  51.         t.setTrade_type(”JSAPI”);//交易类型  
  52.         t.setOpenid(”********************”);//用户标示  
  53.         Map<String, String> map = TyxdUtil.sendTyxdMessage(TyxdUtil.makeTyxdMessage(t));  
  54.         System.out.println(map);  
  55.     }  
  56.       
  57.       
  58. }  
/**
 * 统一下单工具类
 * @author why
 *
 */
public class TyxdUtil {
    /**
     * json数据
     * @param tyxd
     * @return
     */
    public static String makeTyxdMessage(Tyxd tyxd){
        SortedMap<Object,Object> parameters = new TreeMap<Object,Object>();
        parameters.put("appid", tyxd.getAppid());
        parameters.put("mch_id", tyxd.getMch_id());
        parameters.put("nonce_str", tyxd.getNonce_str());
        parameters.put("body", tyxd.getBody());
        parameters.put("out_trade_no", tyxd.getOut_trade_no());
        parameters.put("total_fee", tyxd.getTotal_fee());
        parameters.put("spbill_create_ip",tyxd.getSpbill_create_ip());
        parameters.put("notify_url", tyxd.getNotify_url());
        parameters.put("trade_type", tyxd.getTrade_type());
        parameters.put("openid", tyxd.getOpenid());
        String sign = PayCommonUtil.createSign("UTF-8", parameters);
        parameters.put("sign", sign);
        String requestXML = PayCommonUtil.getRequestXml(parameters);
        return requestXML;
    }
    /**
     * 调用统一下单接口
     * @param requestXML
     * @return
     * @throws Exception
     */
    public static Map<String, String> sendTyxdMessage(String requestXML) throws Exception{
        String result =CommonUtil.httpsRequest(ConfigUtil.UNIFIED_ORDER_URL, "POST", requestXML);
        Map<String, String> map = XMLUtil.doXMLParse(result);//解析微信返回的信息,以Map形式存储便于取值
        return map;
    }
    public static void main(String[] args) throws Exception {
        Tyxd t = new Tyxd();
        t.setAppid(ConfigUtil.APPID);//公众号id
        t.setMch_id(ConfigUtil.MCH_ID);//商户号
        t.setNonce_str(PayCommonUtil.CreateNoncestr());//随机字符串
//      t.setSign(sign);
        t.setBody("支付测试");//商品描述
        t.setOut_trade_no("1441332471849");//订单号
        t.setTotal_fee("1");//总金额
        t.setSpbill_create_ip("127.0.0.1");//终端ip
        t.setNotify_url(ConfigUtil.NOTIFY_URL);//通知地址
        t.setTrade_type("JSAPI");//交易类型
        t.setOpenid("********************");//用户标示
        Map<String, String> map = TyxdUtil.sendTyxdMessage(TyxdUtil.makeTyxdMessage(t));
        System.out.println(map);
    }


}

  1. public class PayCommonUtil {  
  2.     private static Logger log = LoggerFactory.getLogger(PayCommonUtil.class);  
  3.     public static String CreateNoncestr(int length) {  
  4.         String chars = ”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;  
  5.         String res = ”“;  
  6.         for (int i = 0; i < length; i++) {  
  7.             Random rd = new Random();  
  8.             res += chars.indexOf(rd.nextInt(chars.length() - 1));  
  9.         }  
  10.         return res;  
  11.     }  
  12.   
  13.     public static String CreateNoncestr() {  
  14.         String chars = ”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”;  
  15.         String res = ”“;  
  16.         for (int i = 0; i < 16; i++) {  
  17.             Random rd = new Random();  
  18.             res += chars.charAt(rd.nextInt(chars.length() - 1));  
  19.         }  
  20.         return res;  
  21.     }  
  22.       
  23.     /** 
  24.      * 获取一定长度的随机字符串 
  25.      * @param length 指定字符串长度 
  26.      * @return 一定长度的字符串 
  27.      */  
  28.     public static String getRandomStringByLength(int length) {  
  29.         String base = ”abcdefghijklmnopqrstuvwxyz0123456789”;  
  30.         Random random = new Random();  
  31.         StringBuffer sb = new StringBuffer();  
  32.         for (int i = 0; i < length; i++) {  
  33.             int number = random.nextInt(base.length());  
  34.             sb.append(base.charAt(number));  
  35.         }  
  36.         return sb.toString();  
  37.     }  
  38.     /** 
  39.      * 格式为yyyyMMddHHmmss的订单时间 
  40.      * @return 
  41.      */  
  42.     public static String getOrderTime(){  
  43.         SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmss”);  
  44.         return sdf.format(new Date());  
  45.     }  
  46.     /** 
  47.      *时间戳 
  48.      */  
  49.     public static String getTimeStamp(){  
  50.         Long date = System.currentTimeMillis();  
  51.         return date.toString().substring(0, date.toString().length()-3);  
  52.     }  
  53.       
  54.     /** 
  55.      * @author why 
  56.      * @Description:sign签名 
  57.      * @param characterEncoding 编码格式 
  58.      * @param parameters 请求参数 
  59.      * @return 
  60.      */  
  61.     public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){  
  62.         System.out.println(parameters);  
  63.         StringBuffer sb = new StringBuffer();  
  64.         Set es = parameters.entrySet();  
  65.         Iterator it = es.iterator();  
  66.         while(it.hasNext()) {  
  67.             Map.Entry entry = (Map.Entry)it.next();  
  68.             String k = (String)entry.getKey();  
  69.             Object v = entry.getValue();  
  70.             if(null != v && !“”.equals(v)   
  71.                     && !”sign”.equals(k) && !“key”.equals(k)) {  
  72.                 sb.append(k + ”=” + v + “&”);  
  73.             }  
  74.         }  
  75.         sb.append(”key=” + ConfigUtil.API_KEY);  
  76.         System.out.println(sb.toString());  
  77.         String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();  
  78.         return sign;  
  79.     }  
  80.     /** 
  81.      * @author why 
  82.      * @Description:将请求参数转换为xml格式的string 
  83.      * @param parameters  请求参数 
  84.      * @return 
  85.      */  
  86.     public static String getRequestXml(SortedMap<Object,Object> parameters){  
  87.         StringBuffer sb = new StringBuffer();  
  88.         sb.append(”<xml>”);  
  89.         Set es = parameters.entrySet();  
  90.         Iterator it = es.iterator();  
  91.         while(it.hasNext()) {  
  92.             Map.Entry entry = (Map.Entry)it.next();  
  93.             String k = (String)entry.getKey();  
  94.             String v = (String)entry.getValue();  
  95.             if (“attach”.equalsIgnoreCase(k)||“body”.equalsIgnoreCase(k)||“sign”.equalsIgnoreCase(k)) {  
  96.                 sb.append(”<”+k+“>”+“<![CDATA[“+v+“]]></”+k+“>”);  
  97.             }else {  
  98.                 sb.append(”<”+k+“>”+v+“</”+k+“>”);  
  99.             }  
  100.         }  
  101.         sb.append(”</xml>”);  
  102.         return sb.toString();  
  103.     }  
  104.     /** 
  105.      * @author why 
  106.      * @Description:返回给微信的参数 
  107.      * @param return_code 返回编码 
  108.      * @param return_msg  返回信息 
  109.      * @return 
  110.      */  
  111.     public static String setXML(String return_code, String return_msg) {  
  112.         return “<xml><return_code><![CDATA[“ + return_code  
  113.                 + ”]]></return_code><return_msg><![CDATA[“ + return_msg  
  114.                 + ”]]></return_msg></xml>”;  
  115.     }  
  116. }  
public class PayCommonUtil {
    private static Logger log = LoggerFactory.getLogger(PayCommonUtil.class);
    public static String CreateNoncestr(int length) {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String res = "";
        for (int i = 0; i < length; i++) {
            Random rd = new Random();
            res += chars.indexOf(rd.nextInt(chars.length() - 1));
        }
        return res;
    }

    public static String CreateNoncestr() {
        String chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String res = "";
        for (int i = 0; i < 16; i++) {
            Random rd = new Random();
            res += chars.charAt(rd.nextInt(chars.length() - 1));
        }
        return res;
    }

    /**
     * 获取一定长度的随机字符串
     * @param length 指定字符串长度
     * @return 一定长度的字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    /**
     * 格式为yyyyMMddHHmmss的订单时间
     * @return
     */
    public static String getOrderTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
    /**
     *时间戳
     */
    public static String getTimeStamp(){
        Long date = System.currentTimeMillis();
        return date.toString().substring(0, date.toString().length()-3);
    }

    /**
     * @author why
     * @Description:sign签名
     * @param characterEncoding 编码格式
     * @param parameters 请求参数
     * @return
     */
    public static String createSign(String characterEncoding,SortedMap<Object,Object> parameters){
        System.out.println(parameters);
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v) 
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + ConfigUtil.API_KEY);
        System.out.println(sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }
    /**
     * @author why
     * @Description:将请求参数转换为xml格式的string
     * @param parameters  请求参数
     * @return
     */
    public static String getRequestXml(SortedMap<Object,Object> parameters){
        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            String v = (String)entry.getValue();
            if ("attach".equalsIgnoreCase(k)||"body".equalsIgnoreCase(k)||"sign".equalsIgnoreCase(k)) {
                sb.append("<"+k+">"+"<![CDATA["+v+"]]></"+k+">");
            }else {
                sb.append("<"+k+">"+v+"</"+k+">");
            }
        }
        sb.append("</xml>");
        return sb.toString();
    }
    /**
     * @author why
     * @Description:返回给微信的参数
     * @param return_code 返回编码
     * @param return_msg  返回信息
     * @return
     */
    public static String setXML(String return_code, String return_msg) {
        return "<xml><return_code><![CDATA[" + return_code
                + "]]></return_code><return_msg><![CDATA[" + return_msg
                + "]]></return_msg></xml>";
    }
}

  1. /** 
  2.  * 通用工具类 
  3.  * @author why 
  4.  * @date 2014-11-21下午9:10:30 
  5.  */  
  6. public class CommonUtil {  
  7.     private static Logger log = LoggerFactory.getLogger(CommonUtil.class);  
  8.     /** 
  9.      * 发送https请求 
  10.      * @param requestUrl 请求地址 
  11.      * @param requestMethod 请求方式(GET、POST) 
  12.      * @param outputStr 提交的数据 
  13.      * @return 返回微信服务器响应的信息 
  14.      */  
  15.     public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {  
  16.         try {  
  17.             // 创建SSLContext对象,并使用我们指定的信任管理器初始化  
  18.             TrustManager[] tm = { new MyX509TrustManager() };  
  19.             SSLContext sslContext = SSLContext.getInstance(”SSL”“SunJSSE”);  
  20.             sslContext.init(null, tm, new java.security.SecureRandom());  
  21.             // 从上述SSLContext对象中得到SSLSocketFactory对象  
  22.             SSLSocketFactory ssf = sslContext.getSocketFactory();  
  23.             URL url = new URL(requestUrl);  
  24.             HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();  
  25.             conn.setSSLSocketFactory(ssf);  
  26.             conn.setDoOutput(true);  
  27.             conn.setDoInput(true);  
  28.             conn.setUseCaches(false);  
  29.             // 设置请求方式(GET/POST)  
  30.             conn.setRequestMethod(requestMethod);  
  31.             conn.setRequestProperty(”content-type”“application/x-www-form-urlencoded”);   
  32.             // 当outputStr不为null时向输出流写数据  
  33.             if (null != outputStr) {  
  34.                 OutputStream outputStream = conn.getOutputStream();  
  35.                 // 注意编码格式  
  36.                 outputStream.write(outputStr.getBytes(”UTF-8”));  
  37.                 outputStream.close();  
  38.             }  
  39.             // 从输入流读取返回内容  
  40.             InputStream inputStream = conn.getInputStream();  
  41.             InputStreamReader inputStreamReader = new InputStreamReader(inputStream, “utf-8”);  
  42.             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
  43.             String str = null;  
  44.             StringBuffer buffer = new StringBuffer();  
  45.             while ((str = bufferedReader.readLine()) != null) {  
  46.                 buffer.append(str);  
  47.             }  
  48.             // 释放资源  
  49.             bufferedReader.close();  
  50.             inputStreamReader.close();  
  51.             inputStream.close();  
  52.             inputStream = null;  
  53.             conn.disconnect();  
  54.             return buffer.toString();  
  55.         } catch (ConnectException ce) {  
  56.             log.error(”连接超时:{}”, ce);  
  57.         } catch (Exception e) {  
  58.             log.error(”https请求异常:{}”, e);  
  59.         }  
  60.         return null;  
  61.     }  
  62.       
  63.     public static String urlEncodeUTF8(String source){  
  64.         String result = source;  
  65.         try {  
  66.             result = java.net.URLEncoder.encode(source,”utf-8”);  
  67.         } catch (UnsupportedEncodingException e) {  
  68.             e.printStackTrace();  
  69.         }  
  70.         return result;  
  71.     }  
  72. }  
/**
 * 通用工具类
 * @author why
 * @date 2014-11-21下午9:10:30
 */
public class CommonUtil {
    private static Logger log = LoggerFactory.getLogger(CommonUtil.class);
    /**
     * 发送https请求
     * @param requestUrl 请求地址
     * @param requestMethod 请求方式(GET、POST)
     * @param outputStr 提交的数据
     * @return 返回微信服务器响应的信息
     */
    public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
        try {
            // 创建SSLContext对象,并使用我们指定的信任管理器初始化
            TrustManager[] tm = { new MyX509TrustManager() };
            SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
            sslContext.init(null, tm, new java.security.SecureRandom());
            // 从上述SSLContext对象中得到SSLSocketFactory对象
            SSLSocketFactory ssf = sslContext.getSocketFactory();
            URL url = new URL(requestUrl);
            HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
            conn.setSSLSocketFactory(ssf);
            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) {
            log.error("连接超时:{}", ce);
        } catch (Exception e) {
            log.error("https请求异常:{}", e);
        }
        return null;
    }

    public static String urlEncodeUTF8(String source){
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}

  1. public class XMLUtil {  
  2.     /** 
  3.      * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。 
  4.      * @param strxml 
  5.      * @return 
  6.      * @throws JDOMException 
  7.      * @throws IOException 
  8.      */  
  9.     public static Map doXMLParse(String strxml) throws JDOMException, IOException {  
  10.         strxml = strxml.replaceFirst(”encoding=\”.*\”““encoding=\”UTF-8\”“);  
  11.   
  12.         if(null == strxml || “”.equals(strxml)) {  
  13.             return null;  
  14.         }  
  15.           
  16.         Map m = new HashMap();  
  17.           
  18.         InputStream in = new ByteArrayInputStream(strxml.getBytes(“UTF-8”));  
  19.         SAXBuilder builder = new SAXBuilder();  
  20.         Document doc = builder.build(in);  
  21.         Element root = doc.getRootElement();  
  22.         List list = root.getChildren();  
  23.         Iterator it = list.iterator();  
  24.         while(it.hasNext()) {  
  25.             Element e = (Element) it.next();  
  26.             String k = e.getName();  
  27.             String v = ”“;  
  28.             List children = e.getChildren();  
  29.             if(children.isEmpty()) {  
  30.                 v = e.getTextNormalize();  
  31.             } else {  
  32.                 v = XMLUtil.getChildrenText(children);  
  33.             }  
  34.               
  35.             m.put(k, v);  
  36.         }  
  37.           
  38.         //关闭流  
  39.         in.close();  
  40.           
  41.         return m;  
  42.     }  
  43.       
  44.     /** 
  45.      * 获取子结点的xml 
  46.      * @param children 
  47.      * @return String 
  48.      */  
  49.     public static String getChildrenText(List children) {  
  50.         StringBuffer sb = new StringBuffer();  
  51.         if(!children.isEmpty()) {  
  52.             Iterator it = children.iterator();  
  53.             while(it.hasNext()) {  
  54.                 Element e = (Element) it.next();  
  55.                 String name = e.getName();  
  56.                 String value = e.getTextNormalize();  
  57.                 List list = e.getChildren();  
  58.                 sb.append(”<” + name + “>”);  
  59.                 if(!list.isEmpty()) {  
  60.                     sb.append(XMLUtil.getChildrenText(list));  
  61.                 }  
  62.                 sb.append(value);  
  63.                 sb.append(”</” + name + “>”);  
  64.             }  
  65.         }  
  66.           
  67.         return sb.toString();  
  68.     }  
  69.       
  70. }  
public class XMLUtil {
    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点,则此节点的值是子节点的xml数据。
     * @param strxml
     * @return
     * @throws JDOMException
     * @throws IOException
     */
    public static Map doXMLParse(String strxml) throws JDOMException, IOException {
        strxml = strxml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");

        if(null == strxml || "".equals(strxml)) {
            return null;
        }

        Map m = new HashMap();

        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.getTextNormalize();
            } else {
                v = XMLUtil.getChildrenText(children);
            }

            m.put(k, v);
        }

        //关闭流
        in.close();

        return m;
    }

    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    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(XMLUtil.getChildrenText(list));
                }
                sb.append(value);
                sb.append("</" + name + ">");
            }
        }

        return sb.toString();
    }

}

  1. /** 
  2.  * 统一下单实体类 
  3.  * @author why 
  4.  * 
  5.  */  
  6. public class Tyxd {  
  7.     private String appid;//公众账号  
  8.     private String mch_id;//商户号  
  9.     private String nonce_str;//随机字符串  
  10.     private String sign;//签名  
  11.     private String body;//商品描述  
  12.     private String out_trade_no;//商户订单号  
  13.     private String total_fee;//总金额  
  14.     private String spbill_create_ip;//终端ip  
  15.     private String notify_url;//通知地址  
  16.     private String trade_type;//交易类型  
  17.     private String openid;//用户标示  
  18.     public Tyxd() {  
  19.         super();  
  20.     }  
  21.     public Tyxd(String appid, String mch_id, String nonce_str, String sign,  
  22.             String body, String out_trade_no, String total_fee,  
  23.             String spbill_create_ip, String notify_url, String trade_type,  
  24.             String openid) {  
  25.         super();  
  26.         this.appid = appid;  
  27.         this.mch_id = mch_id;  
  28.         this.nonce_str = nonce_str;  
  29.         this.sign = sign;  
  30.         this.body = body;  
  31.         this.out_trade_no = out_trade_no;  
  32.         this.total_fee = total_fee;  
  33.         this.spbill_create_ip = spbill_create_ip;  
  34.         this.notify_url = notify_url;  
  35.         this.trade_type = trade_type;  
  36.         this.openid = openid;  
  37.     }  
  38.     public String getAppid() {  
  39.         return appid;  
  40.     }  
  41.     public void setAppid(String appid) {  
  42.         this.appid = appid;  
  43.     }  
  44.     public String getMch_id() {  
  45.         return mch_id;  
  46.     }  
  47.     public void setMch_id(String mch_id) {  
  48.         this.mch_id = mch_id;  
  49.     }  
  50.     public String getNonce_str() {  
  51.         return nonce_str;  
  52.     }  
  53.     public void setNonce_str(String nonce_str) {  
  54.         this.nonce_str = nonce_str;  
  55.     }  
  56.     public String getSign() {  
  57.         return sign;  
  58.     }  
  59.     public void setSign(String sign) {  
  60.         this.sign = sign;  
  61.     }  
  62.     public String getBody() {  
  63.         return body;  
  64.     }  
  65.     public void setBody(String body) {  
  66.         this.body = body;  
  67.     }  
  68.     public String getOut_trade_no() {  
  69.         return out_trade_no;  
  70.     }  
  71.     public void setOut_trade_no(String out_trade_no) {  
  72.         this.out_trade_no = out_trade_no;  
  73.     }  
  74.     public String getTotal_fee() {  
  75.         return total_fee;  
  76.     }  
  77.     public void setTotal_fee(String total_fee) {  
  78.         this.total_fee = total_fee;  
  79.     }  
  80.     public String getSpbill_create_ip() {  
  81.         return spbill_create_ip;  
  82.     }  
  83.     public void setSpbill_create_ip(String spbill_create_ip) {  
  84.         this.spbill_create_ip = spbill_create_ip;  
  85.     }  
  86.     public String getNotify_url() {  
  87.         return notify_url;  
  88.     }  
  89.     public void setNotify_url(String notify_url) {  
  90.         this.notify_url = notify_url;  
  91.     }  
  92.     public String getTrade_type() {  
  93.         return trade_type;  
  94.     }  
  95.     public void setTrade_type(String trade_type) {  
  96.         this.trade_type = trade_type;  
  97.     }  
  98.     public String getOpenid() {  
  99.         return openid;  
  100.     }  
  101.     public void setOpenid(String openid) {  
  102.         this.openid = openid;  
  103.     }  
  104. }  
/**
 * 统一下单实体类
 * @author why
 *
 */
public class Tyxd {
    private String appid;//公众账号
    private String mch_id;//商户号
    private String nonce_str;//随机字符串
    private String sign;//签名
    private String body;//商品描述
    private String out_trade_no;//商户订单号
    private String total_fee;//总金额
    private String spbill_create_ip;//终端ip
    private String notify_url;//通知地址
    private String trade_type;//交易类型
    private String openid;//用户标示
    public Tyxd() {
        super();
    }
    public Tyxd(String appid, String mch_id, String nonce_str, String sign,
            String body, String out_trade_no, String total_fee,
            String spbill_create_ip, String notify_url, String trade_type,
            String openid) {
        super();
        this.appid = appid;
        this.mch_id = mch_id;
        this.nonce_str = nonce_str;
        this.sign = sign;
        this.body = body;
        this.out_trade_no = out_trade_no;
        this.total_fee = total_fee;
        this.spbill_create_ip = spbill_create_ip;
        this.notify_url = notify_url;
        this.trade_type = trade_type;
        this.openid = openid;
    }
    public String getAppid() {
        return appid;
    }
    public void setAppid(String appid) {
        this.appid = appid;
    }
    public String getMch_id() {
        return mch_id;
    }
    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }
    public String getNonce_str() {
        return nonce_str;
    }
    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }
    public String getSign() {
        return sign;
    }
    public void setSign(String sign) {
        this.sign = sign;
    }
    public String getBody() {
        return body;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public String getOut_trade_no() {
        return out_trade_no;
    }
    public void setOut_trade_no(String out_trade_no) {
        this.out_trade_no = out_trade_no;
    }
    public String getTotal_fee() {
        return total_fee;
    }
    public void setTotal_fee(String total_fee) {
        this.total_fee = total_fee;
    }
    public String getSpbill_create_ip() {
        return spbill_create_ip;
    }
    public void setSpbill_create_ip(String spbill_create_ip) {
        this.spbill_create_ip = spbill_create_ip;
    }
    public String getNotify_url() {
        return notify_url;
    }
    public void setNotify_url(String notify_url) {
        this.notify_url = notify_url;
    }
    public String getTrade_type() {
        return trade_type;
    }
    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }
    public String getOpenid() {
        return openid;
    }
    public void setOpenid(String openid) {
        this.openid = openid;
    }
}

document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
    <div id="digg" articleid="69258069">
        <dl id="btnDigg" class="digg digg_enable" onclick="btndigga();">

             <dt>顶</dt>
            <dd>2</dd>
        </dl>


        <dl id="btnBury" class="digg digg_enable" onclick="btnburya();">

              <dt>踩</dt>
            <dd>0</dd>               
        </dl>

    </div>
 <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank">&nbsp;</a>   </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank">&nbsp;</a></div>
<script type="text/javascript">
    function btndigga() {
        $(".tracking-ad[data-mod='popu_222'] a").click();
    }
    function btnburya() {
        $(".tracking-ad[data-mod='popu_223'] a").click();
    }
        </script>


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值