获取微信用户信息

xiaoxiao2021-02-28  113

参考学习别人的,项目中使用还可以,如有问标题,在下面留言。里面一些关于微信涉及其他请求信息,参考我其他微信相关文章 window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx575sdfsdffsdfs&redirect_uri=http://www.baidu.com/html/index.html&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"; //跳转其他界面传入code //index.html界面用 var code= window.location.href;获取code /**  *访问调用 必须是微信公众号进入一下链接才可  * https://open.weixin.qq.com/connect/oauth2/authorize? appid=wx575sdfsdffsdfs&redirect_uri=http://www.baidu.com/weixin/getOpenId&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect  * */ @RequestMapping("/getOpenId") public JSONObject index(HttpServletRequest request,HttpServletResponse response,String code,String userId) throws Exception{ //String code = request.getParameter("code"); //返回用户微信信息 JSONObject wxUserInfo=accessTokenService.getuserInfoStr(code); //自己创建WeixinUser类用以存入微信用户信息 WeixinUser WeixinUser=(WeixinUser)JSONObject.toJavaObject(wxUserInfo, WeixinUser.class); WeixinUser.setUserId(userId); //cun accessTokenService.SaveWXinfo(WeixinUser); return wxUserInfo; //return ""; } /** 获取用户微信信息 */ public JSONObject getuserInfoStr(String code) throws Exception { String appid = Constant.ceAppId; String secret = Constant.cesecret; String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code"; // 第一次请求 获取access_token 和 openid JSONObject oppidObj = HttpSend.httpsRequest(requestUrl, "GET", null); String access_token = (String) oppidObj.get("access_token"); String openid = (String) oppidObj.get("openid"); String requestUrl2 = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN"; JSONObject wxUserInfo = HttpSend.httpsRequest(requestUrl2, "GET", null); //SaveWXinfo(wxUserInfo); return wxUserInfo; } //http方法 package com.carlock.util; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.log4j.Logger; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import java.io.*; public class HttpSend { Logger logger = Logger.getLogger(this.getClass()); /* public static String getSend(String strUrl,String param){ URL url = null; HttpURLConnection connection = null; try { url = new URL(strUrl + "?" + param); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("GET"); connection.setUseCaches(false); connection.connect(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); return null; } finally { if (connection != null) { connection.disconnect(); } } } */ /// public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; try { TrustManager[] tm = { new JEEWeiXinX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); 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); conn.setRequestMethod(requestMethod); 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(); jsonObject = JSON.parseObject(buffer.toString()); } catch (Exception e) { e.printStackTrace(); } return jsonObject; } /* public static String postSend(String strUrl,String param){ URL url = null; HttpURLConnection connection = null; try { url = new URL(strUrl); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.connect(); //POST方法时使用 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(param); out.flush(); out.close(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { buffer.append(line); } reader.close(); return buffer.toString(); } catch (IOException e) { e.printStackTrace(); return null; } finally { if (connection != null) { connection.disconnect(); } } } */ /**  * 转为16进制方法  * @param str  * @return  * @throws UnsupportedEncodingException   */ public static String paraTo16(String str) throws UnsupportedEncodingException { String hs = ""; byte[] byStr = str.getBytes("UTF-8"); for(int i=0;i<byStr.length;i++) { String temp = ""; temp = (Integer.toHexString(byStr[i]&0xFF)); if(temp.length()==1) temp = "%0"+temp; else temp = "%"+temp; hs = hs+temp; } return hs.toUpperCase(); } public static void main(String[] args) { System.out.println(System.currentTimeMillis()); } } class JEEWeiXinX509TrustManager implements X509TrustManager { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }
转载请注明原文地址: https://www.6miu.com/read-61790.html

最新回复(0)