标签:pcl 网页 ESS gen get net state inf .json
相信官方文档,以前的接口错误已经改正,用最新的即可,这几篇随笔是一个项目中拿出来的,缺什么方法去其他随笔里找找就有
1 import java.io.IOException; 2 import java.io.UnsupportedEncodingException; 3 import javax.servlet.ServletException; 4 import java.net.URLEncoder; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import org.apache.http.HttpEntity; 12 import org.apache.http.HttpResponse; 13 import org.apache.http.client.methods.HttpGet; 14 import org.apache.http.impl.client.DefaultHttpClient; 15 import org.apache.http.util.EntityUtils; 16 import org.slf4j.Logger; 17 import org.slf4j.LoggerFactory; 18 import org.springframework.stereotype.Controller; 19 import org.springframework.web.bind.annotation.RequestMapping; 20 import org.springframework.web.bind.annotation.ResponseBody; 21 22 import com.fuyin.mp.utils.GetMenuJson; 23 import com.fuyin.mp.utils.NetWorkHelper; 24 import com.fuyin.mp.utils.WxaApi; 25 import com.google.gson.Gson; 26 27 import net.sf.json.JSONArray; 28 import net.sf.json.JSONObject; 29 30 @Controller 31 public class WxController { 32 private static final Logger logger = LoggerFactory.getLogger(WxController.class); 33 NetWorkHelper netWorkHelper=new NetWorkHelper(); 34 /** 35 * 创建自定义菜单栏 36 */ 37 @RequestMapping("creatmenus.action") 38 public String creatmenus(HttpServletRequest req,HttpServletResponse res){ 39 logger.debug("自定义菜单栏"); 40 NetWorkHelper netWorkHelper=new NetWorkHelper(); 41 String u1=WxaApi.CreateMenus.replace("TOKEN", WxaApi.accessToken.getAccessToken()); 42 String getruslt = netWorkHelper.getHttpsResponse(u1, "POST", new GetMenuJson().getMenuJson()); 43 logger.debug("自定义菜单栏返回结果="+getruslt); 44 return "index.jsp"; 45 } 46 47 /** 48 * 获取用户授权 49 */ 50 @RequestMapping("Oauth.get") 51 public String Oauth2(HttpServletRequest request,HttpServletResponse response){ 52 String newopenid = null; 53 String nickname = null; 54 String city = null; 55 try { 56 logger.debug("授权后的回调请求处理"); 57 request.setCharacterEncoding("utf-8"); 58 response.setCharacterEncoding("utf-8"); 59 // 用户同意授权后,能获取到code 60 String code = request.getParameter("code"); 61 String state = request.getParameter("state"); 62 // 用户同意授权 63 if (!"authdeny".equals(code)) { 64 //通过code换取网页授权access_token 65 String reqUrl=WxaApi.codetotoken.replace("APPID", WxaApi.appID).replace("SECRET",WxaApi.appsecret).replace("CODE", code); 66 String httpsResponse = netWorkHelper.getHttpsResponse(reqUrl, "GET", ""); 67 logger.debug("用户授权返回的TOKEN="+httpsResponse); 68 JSONObject jsonObject=JSONObject.fromObject(httpsResponse); 69 String access_token = jsonObject.getString("access_token"); 70 String openid = jsonObject.getString("openid"); 71 String url=WxaApi.getinfor.replace("ACCESS_TOKEN",access_token).replace("OPENID", openid); 72 JSONObject jsonbject = null; 73 DefaultHttpClient client = new DefaultHttpClient(); 74 HttpGet httpget = new HttpGet(url); 75 HttpResponse respone = client.execute(httpget); 76 HttpEntity entity = respone.getEntity(); 77 if(entity != null){ 78 String result = EntityUtils.toString(entity,"UTF-8"); 79 jsonbject = JSONObject.fromObject(result); 80 } 81 newopenid = jsonbject.getString("openid"); 82 city = jsonbject.getString("city"); 83 nickname = jsonbject.getString("nickname"); 84 logger.debug("用户授权返回的用户信息="+jsonbject); 85 nickname = URLEncoder.encode(nickname,"utf-8"); 86 System.out.println("openid="+openid+"----nickname="+nickname); 87 } 88 89 } catch (UnsupportedEncodingException e) { 90 // TODO Auto-generated catch block 91 e.printStackTrace(); 92 }catch (IOException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } 96 // 跳转到index.jsp 97 return "redirect:"+WxaApi.WebUrl+"/demo/main.html?openid="+newopenid+"&nickname="+nickname; 98 } 99 }
授权以后通过重定向,去另一个域名的前端项目里,以后的交互全是跨域
标签:pcl 网页 ESS gen get net state inf .json
原文地址:https://www.cnblogs.com/chaoswu/p/10174527.html