:第一步

/ js动态获取?后面的参数,并且封装成一个json对象 * @returns {Object}*/ function getParam(){ var url=location.search; var param = new Object(); if(url.indexOf("?")!=-1){ var str = url.substr(1) strs = str.split("&"); for(var i=0;i

:第二步
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import java.io.IOException; public class HttpClientUtils {
/ * 发送get请求 * @param url 请求地址 * @return 返回内容 json */ public static String httpGet(String url){
// 1 创建发起请求客户端 try {
HttpClient client = new HttpClient(); // 2 创建要发起请求-tet GetMethod getMethod = new GetMethod(url); // getMethod.addRequestHeader("Content-Type", // "application/x-www-form-urlencoded;charset=UTF-8"); getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf8"); // 3 通过客户端传入请求就可以发起请求,获取响应对象 client.executeMethod(getMethod); // 4 提取响应json字符串返回 String result = new String(getMethod.getResponseBodyAsString().getBytes("utf8")); return result; } catch (IOException e) {
e.printStackTrace(); } return null; } }
<!--处理json--> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>
//前端描码拿到code String code = map.get("code"); //准备发送get请求 获取openid 替换接口网址里面的字段 String url = WxConstants.GET_ACK_URL.replace("APPID", WxConstants.APPID) .replace("SECRET", WxConstants.SECRET).replace("CODE", code); //发送get请求 返回access_token 拿到openid和unionid String httpGet = HttpClientUtils.httpGet(url); //json字符串转换成json对象 JSONObject jsonObject = JSON.parseObject(httpGet); //从json对象中获取openid和access_token String openid = jsonObject.getString("openid"); String access_token = jsonObject.getString("access_token");
:第三步
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/204323.html原文链接:https://javaforall.net
