New file |
| | |
| | | package com.dg.core.util; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.dg.core.db.manual.mapper.util.ConstantPropertiesUtil; |
| | | import org.apache.http.HttpResponse; |
| | | import org.apache.http.client.methods.HttpGet; |
| | | import org.apache.http.impl.client.DefaultHttpClient; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | public class WxUtil { |
| | | |
| | | private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"; |
| | | |
| | | /** |
| | | * 获取花城token,(ps:0=token获取失败) |
| | | * |
| | | * @return |
| | | */ |
| | | public String getBatteryCarAccessToken() throws Exception { |
| | | String accessToken = "0"; |
| | | try { |
| | | // 此处APP_ID APP_SECRET 在微信小程序后端可见 |
| | | // String accessTokenUrl = String.format(TEMP_URL, APP_ID, APP_SECRET); |
| | | String accessTokenUrl = ACCESS_TOKEN_URL + "&appid=" + ConstantPropertiesUtil.WX_OPEN_APP_ID |
| | | + "&secret=" + ConstantPropertiesUtil.WX_OPEN_APP_SECRET; |
| | | String result = this.httpGet(accessTokenUrl, null, null); |
| | | Map<String, Object> resultMap = JSON.parseObject(result, Map.class); |
| | | if (resultMap.containsKey("access_token")) { |
| | | accessToken = resultMap.get("access_token").toString(); |
| | | } |
| | | } catch (IOException ioe) { |
| | | ioe.printStackTrace(); |
| | | } |
| | | return accessToken; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * http请求工具类,get请求 |
| | | * |
| | | * @param url |
| | | * @param params |
| | | * @param resonseCharSet |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static String httpGet(String url, Map<String, Object> params, String... resonseCharSet) throws Exception { |
| | | DefaultHttpClient defaultHttpClient = null; |
| | | BufferedReader bufferedReader = null; |
| | | try { |
| | | defaultHttpClient = new DefaultHttpClient(); |
| | | if (params != null) { |
| | | StringBuilder stringBuilder = new StringBuilder(); |
| | | Iterator<String> iterator = params.keySet().iterator(); |
| | | String key; |
| | | while (iterator.hasNext()) { |
| | | key = iterator.next(); |
| | | Object val = params.get(key); |
| | | if (val instanceof List) { |
| | | List v = (List) val; |
| | | for (Object o : v) { |
| | | stringBuilder.append(key).append("=").append(o.toString()).append("&"); |
| | | } |
| | | } else { |
| | | stringBuilder.append(key).append("=").append(val.toString()).append("&"); |
| | | } |
| | | } |
| | | stringBuilder.deleteCharAt(stringBuilder.length() - 1); |
| | | url = url + "?" + stringBuilder.toString(); |
| | | } |
| | | HttpGet httpGet = new HttpGet(url); |
| | | httpGet.setHeader("Content-Type", "application/json;charset=ut-8"); |
| | | HttpResponse httpResponse = defaultHttpClient.execute(httpGet); |
| | | if (httpResponse.getStatusLine().getStatusCode() != 200) { |
| | | String errorLog = "请求失败,errorCode:" + httpResponse.getStatusLine().getStatusCode(); |
| | | |
| | | throw new Exception(url + errorLog); |
| | | } |
| | | // 读取返回信息 |
| | | String charSet = "utf-8"; |
| | | if (resonseCharSet != null && resonseCharSet.length > 0) |
| | | charSet = resonseCharSet[0]; |
| | | String output; |
| | | bufferedReader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), charSet)); |
| | | |
| | | StringBuilder dataBuilder = new StringBuilder(); |
| | | while ((output = bufferedReader.readLine()) != null) { |
| | | dataBuilder.append(output); |
| | | } |
| | | return dataBuilder.toString(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | throw e; |
| | | } finally { |
| | | if (defaultHttpClient != null) |
| | | defaultHttpClient.getConnectionManager().shutdown(); |
| | | if (bufferedReader != null) |
| | | bufferedReader.close(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |