From 9b997e71f9aef4fd602637acf870bfa63e92ec55 Mon Sep 17 00:00:00 2001 From: 陈杰 <504987307@qq.com> Date: Thu, 18 Jun 2020 16:16:12 +0800 Subject: [PATCH] 微信分享 --- src/main/java/com/sincere/student/controller/CommonController.java | 32 ++++++++------------------------ src/main/java/com/sincere/student/utils/HttpClientUtils.java | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/sincere/student/utils/HttpClientUtils.java diff --git a/src/main/java/com/sincere/student/controller/CommonController.java b/src/main/java/com/sincere/student/controller/CommonController.java index 6da72a5..ced86e7 100644 --- a/src/main/java/com/sincere/student/controller/CommonController.java +++ b/src/main/java/com/sincere/student/controller/CommonController.java @@ -9,6 +9,7 @@ import com.sincere.student.model.Area; import com.sincere.student.model.Token; import com.sincere.student.service.CommonService; import com.sincere.student.service.ParameterService; +import com.sincere.student.utils.HttpClientUtils; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; @@ -57,31 +58,9 @@ public class CommonController { WxSign wxSign1=new WxSign(); wxSign1.setTimestamp(Long.toString(System.currentTimeMillis())); wxSign1.setNoncestr(UUID.randomUUID().toString()); - AccessToken accessToken=parameterService.getAccessToken(); - Token token=new Token(); - if(accessToken!=null) { - Calendar dateOne=Calendar.getInstance(); - Calendar dateTwo=Calendar.getInstance(); - dateOne.setTime(new Date()); - dateTwo.setTime(accessToken.getCreateTime()); - long timeOne=dateOne.getTimeInMillis(); - long timeTwo=dateTwo.getTimeInMillis(); - long minute=(timeOne-timeTwo)/(1000*60); + String token = getToken(); - if(minute<60) token.setAccess_token(accessToken.getAccessToken()); - else{ - token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); - accessToken.setAccessToken(token.getAccess_token()); - parameterService.insertAccessToken(accessToken); - } - } - else { - token=getAccessToken("wx6078ff3f67524996","8a0465b8ad0f000f568f48853e2818c8"); - accessToken=new AccessToken(); - accessToken.setAccessToken(token.getAccess_token()); - parameterService.insertAccessToken(accessToken); - } - String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token.getAccess_token()); + String js=Get("https://api.weixin.qq.com/cgi-bin/ticket/getticket","type=jsapi&access_token="+token); JSONObject jsonObject= JSONObject.parseObject(js); wxSign1.setJsapi_ticket(jsonObject.getString("ticket")); String rawstring = "jsapi_ticket=" + wxSign1.getJsapi_ticket() + "&noncestr=" + wxSign1.getNoncestr() + "×tamp=" + wxSign1.getTimestamp() + "&url=" + url + ""; @@ -181,6 +160,11 @@ public class CommonController { return list ; } + + public static String getToken() { + return HttpClientUtils.httpGet2("http://121.40.30.78:9903/getToken"); + } + public String Get(String url, String param) { String result = ""; BufferedReader in = null; diff --git a/src/main/java/com/sincere/student/utils/HttpClientUtils.java b/src/main/java/com/sincere/student/utils/HttpClientUtils.java new file mode 100644 index 0000000..8d6e5ca --- /dev/null +++ b/src/main/java/com/sincere/student/utils/HttpClientUtils.java @@ -0,0 +1,190 @@ +package com.sincere.student.utils; + +import com.alibaba.fastjson.JSONObject; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * HttpClient4.3工具类 + * @author chen + * @version 1.0 + * @date 2019/10/11 0011 10:17 + */ +public class HttpClientUtils { + + private static RequestConfig requestConfig = null; + + static { + // 设置请求和传输超时时间 + requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); + } + + public static void main(String[] args) throws Exception { + + } + + /** + * post请求传输json参数 + * @param url url地址 + * @param jsonParam 参数 + * @return + */ + public static JSONObject httpPostJson(String url, String jsonParam) { + // post请求返回结果 + CloseableHttpClient httpClient = HttpClients.createDefault(); + JSONObject jsonResult = null; + HttpPost httpPost = new HttpPost(url); + // 设置请求和传输超时时间 + httpPost.setConfig(requestConfig); + try { + if (null != jsonParam) { + // 解决中文乱码问题 + StringEntity entity = new StringEntity(jsonParam, "utf-8"); + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/json"); + httpPost.setEntity(entity); + } + CloseableHttpResponse result = httpClient.execute(httpPost); + // 请求发送成功,并得到响应 + if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + String str = ""; + // 读取服务器返回过来的json字符串数据 + str = EntityUtils.toString(result.getEntity(), "utf-8"); + // 把json字符串转换成json对象 + jsonResult = JSONObject.parseObject(str); + + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + httpPost.releaseConnection(); + } + return jsonResult; + } + + /** + * post请求传输String参数 例如:name=Jack&sex=1&type=2 + * Content-type:application/x-www-form-urlencoded + * @param url url地址 + * @param strParam 参数 + * @return + */ + public static JSONObject httpPost(String url, String strParam) { + // post请求返回结果 + CloseableHttpClient httpClient = HttpClients.createDefault(); + JSONObject jsonResult = null; + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + try { + if (null != strParam) { + // 解决中文乱码问题 + StringEntity entity = new StringEntity(strParam, "utf-8"); + entity.setContentEncoding("UTF-8"); + entity.setContentType("application/x-www-form-urlencoded"); + httpPost.setEntity(entity); + } + CloseableHttpResponse result = httpClient.execute(httpPost); + // 请求发送成功,并得到响应 + if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + String str = ""; + // 读取服务器返回过来的json字符串数据 + str = EntityUtils.toString(result.getEntity(), "utf-8"); + // 把json字符串转换成json对象 + jsonResult = JSONObject.parseObject(str); + } + } catch (IOException e) { + + } finally { + httpPost.releaseConnection(); + } + return jsonResult; + } + + /** + * 发送get请求 + * @param url 路径 + * @return + */ + public static JSONObject httpGet(String url) { + // get请求返回结果 + JSONObject jsonResult = null; + CloseableHttpClient client = HttpClients.createDefault(); + // 发送get请求 + HttpGet request = new HttpGet(url); + request.setConfig(requestConfig); + try { + CloseableHttpResponse response = client.execute(request); + // 请求发送成功,并得到响应 + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + // 读取服务器返回过来的json字符串数据 + HttpEntity entity = response.getEntity(); + String strResult = EntityUtils.toString(entity, "utf-8"); + // 把json字符串转换成json对象 + jsonResult = JSONObject.parseObject(strResult); + } + } catch (IOException e) { + + } finally { + request.releaseConnection(); + } + return jsonResult; + } + + public static String httpGet2(String url) { + // get请求返回结果 + JSONObject jsonResult = null; + String result = "" ; + CloseableHttpClient client = HttpClients.createDefault(); + // 发送get请求 + HttpGet request = new HttpGet(url); + request.setConfig(requestConfig); + try { + CloseableHttpResponse response = client.execute(request); + // 请求发送成功,并得到响应 + if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + // 读取服务器返回过来的json字符串数据 + HttpEntity entity = response.getEntity(); + String strResult = EntityUtils.toString(entity, "utf-8"); + // 把json字符串转换成json对象 + //jsonResult = JSONObject.parseObject(strResult); + result = strResult; + } + } catch (IOException e) { + + } finally { + request.releaseConnection(); + } + return result; + } + + + public static InputStream GetFileInputStream(String fileUrl){ + try{ + URL url = new URL(fileUrl); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + //设置超时间为3秒 + conn.setConnectTimeout(8*1000); + //得到输入流 + InputStream inputStream = conn.getInputStream(); + + return inputStream ; + }catch (Exception e){ + + } + return null ; + } + +} -- libgit2 0.21.0