QQ客户端类QQHttpClient:
主要用于QQ消息返回
import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils; import JAVA.io.IOException; public class QQHttpClient {//QQ互联中提供的 appid 和 appkeypublic static final String APPID = "appid";public static final String APPKEY = "appkey";private static JSONObject parseJSONP(String jsonp){int startIndex = jsonp.indexOf("(");int endIndex = jsonp.lastIndexOf(")");String json = jsonp.substring(startIndex + 1,endIndex);return JSONObject.parseobject(json);}//qq返回信息:access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14public static String getAccessToken(String url) throws IOException {CloseableHttpClient client = HttpClients.createDefault();String token = null;HttpGet httpGet = new HttpGet(url);HttpResponse response = client.execute(httpGet);HttpEntity entity = response.getEntity();if(entity != null){String result = EntityUtils.toString(entity,"UTF-8");if(result.indexOf("access_token") >= 0){String[] array = result.split("&");for (String str : array){if(str.indexOf("access_token") >= 0){token = str.substring(str.indexOf("=") + 1);break;}}}}httpGet.releaseConnection();return token;}//qq返回信息:callback( {"client_id":"YOUR_APPID","openid":"YOUR_OPENID"} ); 需要用到上面自己定义的解析方法parseJSONPpublic static String getOpenID(String url) throws IOException {JSONObject jsonObject = null;CloseableHttpClient client = HttpClients.createDefault();HttpGet httpGet = new HttpGet(url);HttpResponse response = client.execute(httpGet);HttpEntity entity = response.getEntity();if(entity != null){String result = EntityUtils.toString(entity,"UTF-8");jsonObject = parseJSONP(result);}httpGet.releaseConnection();if(jsonObject != null){return jsonObject.getString("openid");}else {return null;}}//qq返回信息:{ "ret":0, "msg":"", "nickname":"YOUR_NICK_NAME", ... } , 为JSON格式 , 直接使用JSONObject对象解析public static JSONObject getUserInfo(String url) throws IOException {JSONObject jsonObject = null;CloseableHttpClient client = HttpClients.createDefault();HttpGet httpGet = new HttpGet(url);HttpResponse response = client.execute(httpGet);HttpEntity entity = response.getEntity();if(entity != null){String result = EntityUtils.toString(entity,"UTF-8");jsonObject = JSONObject.parseObject(result);}httpGet.releaseConnection();return jsonObject;}}异常类QQStateErrorException:
public class QQStateErrorException extends Exception {public QQStateErrorException() {super();}public QQStateErrorException(String message) {super(message);}public QQStateErrorException(String message, Throwable cause) {super(message, cause);}public QQStateErrorException(Throwable cause) {super(cause);}protected QQStateErrorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {super(message, cause, enableSuppression, writableStackTrace);}}首页controller用于跳转页面
@Controllerpublic class IndexController {@GetMapping({"/index", "/"})public String index(){return "index";}@GetMapping("/home")public String home(HttpSession session, Model model){String openid = (String) session.getAttribute("openid");String nickname = (String) session.getAttribute("nickname");String figureurl_qq_2 = (String) session.getAttribute("figureurl_qq_2");model.addAttribute("openid",openid);model.addAttribute("nickname",nickname);model.addAttribute("figureurl_qq_2",figureurl_qq_2);return "home";}}还有两个简单的登录页面和信息页面
index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body><a href=https://www.isolves.com/it/cxkf/yy/JAVA/2019-12-26/"/qq/login">QQ登录
