这个app.js的部分内容
App({ onLaunch: function () { //调用API从本地缓存中获取数据 var logs = wx.getStorageSync( 'logs' ) || [] logs.unshift(Date.now()) wx.setStorageSync( 'logs' , logs); this .loginFunction(); }, loginFunction: function (){ wx.checkSession({ // success: function () { // console.log("checksession success") // //session 未过期,并且在本生命周期一直有效 // }, success: function () { wx.login({ success: function (res) { if (res.code) { //发起网络请求 wx.request({ url: "https://myyxa17.hk1.mofasuidao.cn/ServletTest/HelloWorldServlet" , data:{ code:res.code }, success: function (res) { console.log(res); }, fail: function (res) { console.log(res); } }) } else { console.log( '获取用户登录态失败!' + res.errMsg) } } }) } }) }} |
这个是servlet内容
[@WebServlet](/user/WebServlet) ( "/HelloWorldServlet" ) public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * [@see](/user/see) HttpServlet#HttpServlet() */ public HelloWorldServlet() { super (); // TODO Auto-generated constructor stub } /** * [@see](/user/see) HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String code= "" ; code=request.getParameter( "code" ); System.out.println(code); String url = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET" + "&grant_type=authorization_code&js_code=" +code; String wxUserInfoJsonStr = HttpClientUtil.getInstance().sendHttpsGet(url); response.getWriter().append( "Served at: " ).append(wxUserInfoJsonStr); } /** * [@see](/user/see) HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } |
直接在后台打断点,第一次请求就直接返回40163,求解,看别人都是5分钟之内重复使用code才会返回40163,我这个怎么始终返回40163呢,应该怎么修改
必须使用POST请求才能够成功。
public static String sendHttpsPost(String url, Map<String, String> params) throws IOException, GeneralSecurityException {
String responseContent = null;
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<>();
for (Map.Entry<String, String> entry : params.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, Constant.UTF_8));
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()).setDefaultRequestConfig(requestConfig).build();
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
responseContent = EntityUtils.toString(httpEntity, Constant.UTF_8);
}
return responseContent;
}
private static SSLConnectionSocketFactory createSSLConnSocketFactory() throws GeneralSecurityException {
SSLConnectionSocketFactory sslsf = null;
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
sslsf = new SSLConnectionSocketFactory(sslContext);
return sslsf;
}
String urlPath = new String(“https://api.weixin.qq.com/sns/jscode2session”); // 微信提供的API,这里最好也放在配置文件
Map<String, String> params = new HashMap<>();
params.put(“appid”, appId);
params.put(“secret”, secret);
params.put(“js_code”, code);
params.put(“grant_type”, “authorization_code”); // 固定值
params.put(“r”, “”); // 随机值
String data = HttpClientTest.sendHttpsPost(urlPath, params); // java的网络请求,这里是我自己封装的一个工具包,返回字符串