81,092
社区成员
发帖
与我相关
我的任务
分享
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 用户同意授权后,能获取到code
String code =request.getParameter("code").toString();
WeixinOauth2Token weixinOauth2Token = AdvancedUtil.getOauth2AccessToken("", "", code);
// 用户标识
String openId = weixinOauth2Token.getOpenId();
request.setAttribute("openId", "1");
LoanJDBCDao.LoanJDBCDao.getInstance().test(openId);
LoanJDBCDao.LoanJDBCDao.getInstance().test("1");
// 跳转到index.jsp
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
然后用获取的code替换链接中的CODE参数:
public static WeixinOauth2Token getOauth2AccessToken(String appId, String appSecret, String code) {
WeixinOauth2Token wat = null;
String requestUrl = "https://dopen.weimob.com/fuwu/c/oauth2/token?";
String cc= "code=CODE&grant_type=authorization_code&client_id=××&client_secret=××&redirect_uri=http://baidu.com";
cc = cc.replace("CODE",code);
LoanJDBCDao.LoanJDBCDao.getInstance().test("CODE:"+code);
// 获取网页授权凭证֤
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, cc, null);
LoanJDBCDao.LoanJDBCDao.getInstance().test(jsonObject.toString());
wat = new WeixinOauth2Token();
wat.setOpenId(jsonObject.getString("openId"));
return wat;
}
然后调用httpsRequest请求:
public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
JSONObject jsonObject = null;
LoanJDBCDao.LoanJDBCDao.getInstance().test(requestUrl);
try {
// 创建SSLContext对象,并使用我们信任的管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSlContext对象中获取SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setHostnameVerifier(new CommonUtil().new TrustAnyHostnameVerifier());
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(get/post)
conn.setRequestMethod("POST");
conn.connect();
OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
osw.write(requestMethod);
osw.flush();
osw.close();
LoanJDBCDao.LoanJDBCDao.getInstance().test(requestMethod);
LoanJDBCDao.LoanJDBCDao.getInstance().test(requestUrl);
// 从输入流读取返回内容
int retCode = conn.getResponseCode();
InputStream inputStream;
if(retCode >=400)
{
inputStream=conn.getErrorStream();
}
else
inputStream= conn.getInputStream();
//InputStream inputStream = conn.getInputStream();
LoanJDBCDao.LoanJDBCDao.getInstance().test("2");
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 = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
log("连接超时{}"+ce);
} catch (Exception e) {
log("https请求异常:{}"+e);
LoanJDBCDao.LoanJDBCDao.getInstance().test(e.toString());
}
return jsonObject;
}