4f16928cabae3eed1cbca389a5f43c4594666f3a.svn-base 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.yihu.utils;
  2. import java.io.InputStream;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.security.MessageDigest;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.util.Formatter;
  9. import java.util.UUID;
  10. import net.sf.json.JSONObject;
  11. public class WxSDK
  12. {
  13. private final static String APPID="";//暂时没有参数无法自己实现access_token和jsapi_ticket
  14. private final static String APPSECRET="";//暂时没有参数无法自己实现access_token和jsapi_ticket
  15. /* public String access_token(){
  16. String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
  17. String access_token=null;
  18. String expires_in=null;
  19. try{
  20. URL urlGet = new URL(url);
  21. HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
  22. http.setRequestMethod("GET"); // 必须是get方式请求
  23. http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  24. http.setDoOutput(true);
  25. http.setDoInput(true);
  26. System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
  27. System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
  28. http.connect();
  29. InputStream is = http.getInputStream();
  30. int size = is.available();
  31. byte[] jsonBytes = new byte[size];
  32. is.read(jsonBytes);
  33. String message = new String(jsonBytes, "UTF-8");
  34. JSONObject demoJson = JSONObject.fromObject(message);
  35. access_token = demoJson.getString("access_token");
  36. expires_in = demoJson.getString("expires_in");
  37. is.close();
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. return access_token;
  42. }
  43. public String jsapi_ticket(String access_token){
  44. String str = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+access_token+"&type=jsapi";
  45. String jsapi_ticket = null;
  46. String expiresin =null;
  47. try {
  48. URL url = new URL(str);
  49. HttpURLConnection http = (HttpURLConnection) url.openConnection();
  50. http.setRequestMethod("GET"); // 必须是get方式请求
  51. http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
  52. http.setDoOutput(true);
  53. http.setDoInput(true);
  54. System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
  55. System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
  56. http.connect(); InputStream is = http.getInputStream();
  57. int size = is.available();
  58. byte[] jsonBytes = new byte[size];
  59. is.read(jsonBytes);
  60. String message = new String(jsonBytes, "UTF-8");
  61. JSONObject demoJson = JSONObject.fromObject(message);
  62. jsapi_ticket = demoJson.getString("ticket");
  63. expiresin = demoJson.getString("expires_in");
  64. is.close();
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. return jsapi_ticket;
  69. } */
  70. private static String byteToHex(final byte[] hash) {
  71. Formatter formatter = new Formatter();
  72. for (byte b : hash)
  73. {
  74. formatter.format("%02x", b);
  75. }
  76. String result = formatter.toString();
  77. formatter.close();
  78. return result;
  79. }
  80. private static String create_nonce_str() {
  81. return UUID.randomUUID().toString().trim().replaceAll("-", "");
  82. }
  83. private static String create_timestamp() {
  84. return Long.toString(System.currentTimeMillis() / 1000);
  85. }
  86. public static JSONObject getConfig(String jsapi_ticket, String url) {
  87. JSONObject ret = new JSONObject();
  88. String nonce_str = create_nonce_str();
  89. String timestamp = create_timestamp();
  90. String string1;
  91. String signature = "";
  92. string1 = "jsapi_ticket=" + jsapi_ticket +
  93. "&noncestr=" + nonce_str +
  94. "&timestamp=" + timestamp +
  95. "&url=" + url;
  96. try
  97. {
  98. MessageDigest crypt = MessageDigest.getInstance("SHA-1");
  99. crypt.reset();
  100. crypt.update(string1.getBytes("UTF-8"));
  101. signature = byteToHex(crypt.digest());
  102. }
  103. catch (NoSuchAlgorithmException e)
  104. {
  105. e.printStackTrace();
  106. }
  107. catch (UnsupportedEncodingException e)
  108. {
  109. e.printStackTrace();
  110. }
  111. ret.put("url", url);
  112. ret.put("jsapi_ticket", jsapi_ticket);
  113. ret.put("nonceStr", nonce_str);
  114. ret.put("timestamp", timestamp);
  115. ret.put("signature", signature);
  116. return ret;
  117. }
  118. }