1.小程序端wx.login({
success: function (res) {
console.log('code:' + res.code)
//发送请求
wx.request({
url: app.globalData.requestURL, //改成自己的服务器地址
data: {
code: res.code,//上面wx.login()成功获取到的code
style: 'Login'
},
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
},
success: function (res) {
console.log(res)
console.log(res.data.openid)
}
})
}
})
2.java后台
2.1用到的方法@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet--------------------");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("doPost--------------------");
// 获取操作类型,根据类型执行不同操作
String operFlag = request.getParameter("operFlag");
System.out.println("operFlag"+operFlag);
String results = "";
if ("getOpenid".equals(operFlag)) {
String code = request.getParameter("code"); //拿到微信小程序传过来的code
System.out.println(code);
System.out.println("appid"+appid);
System.out.println("secretKey"+secretKey);
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secretKey
+ "&js_code=" + code + "&grant_type=authorization_code"; //接口地址
System.out.println("url"+url);
results = sendGetReq(url);// 发送http请求
System.out.println("results"+results);
}
response.setContentType("application/json;charset=UTF-8");
response.setHeader("catch-control", "no-catch");
PrintWriter out = response.getWriter();
out.write(results);
out.flush();
out.close();
}
private String sendGetReq(String url) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
java.util.Map> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
} // 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
2.2自己的逻辑函数里面添加String code = request.getParameter("code");//获取code
System.out.println("code:"+code);
String urlwx = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secretKey
+ "&js_code=" + code + "&grant_type=authorization_code"; //接口地址
System.out.println("urlwx"+urlwx);
results = sendGetReq(urlwx);// 发送http请求
System.out.println("results"+results);
JSONObject jsonObject = JSONObject.fromObject(results);
System.out.println("jsonObject为:"+jsonObject);
String openid = jsonObject.get("openid").toString();
System.out.println("得到的openid为:"+openid);
response.setContentType("application/json;charset=UTF-8");
response.setHeader("catch-control", "no-catch");
out.write(results);
其中appid和secret根据自己的微信开发工具填写。
运行后即可获得openid