|
|
package yanzheng;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.io.PrintWriter;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.annotation.WebServlet;
|
|
|
import javax.servlet.http.HttpServlet;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@WebServlet("/Yanzheng")
|
|
|
public class Yanzheng extends HttpServlet {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
public Yanzheng() {
|
|
|
super();
|
|
|
// TODO Auto-generated constructor stub
|
|
|
}
|
|
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
doPost(request, response);
|
|
|
}
|
|
|
|
|
|
public static String SendGET(String url,String param){
|
|
|
String result="";//访问返回结果
|
|
|
BufferedReader read=null;//读取访问结果
|
|
|
URL realurl=null;
|
|
|
URLConnection connection=null;
|
|
|
try {
|
|
|
//创建url
|
|
|
realurl=new URL(new StringBuffer(url).toString());
|
|
|
//打开连接
|
|
|
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();
|
|
|
// 获取所有响应头字段
|
|
|
Map<String, List<String>> map = connection.getHeaderFields();
|
|
|
// 遍历所有的响应头字段,获取到cookies等
|
|
|
for (String key : map.keySet()) {
|
|
|
System.out.println(key + "--->" + map.get(key));
|
|
|
}
|
|
|
// 定义 BufferedReader输入流来读取URL的响应
|
|
|
read = new BufferedReader(new InputStreamReader(
|
|
|
connection.getInputStream(),"UTF-8"));
|
|
|
String line;//循环读取
|
|
|
while ((line = read.readLine()) != null) {
|
|
|
result += line;
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}finally{
|
|
|
if(read!=null){//关闭流
|
|
|
try {
|
|
|
read.close();
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
// if(connection != null) {
|
|
|
// connection.
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
|
response.setHeader("Content - Encoding","utf-8");
|
|
|
response.setContentType("text/json; charset=utf-8");
|
|
|
String code =request.getParameter("code");
|
|
|
StringBuffer url = new StringBuffer("https://api.weixin.qq.com/sns/jscode2session?");
|
|
|
url.append("appid=wxdc9f23490d56599c");
|
|
|
url.append("&secret=c39052978a3e80d55ff53227013384ce");
|
|
|
url.append("&js_code="); url.append(code);
|
|
|
url.append("&grant_type=authorization_code");
|
|
|
String finalURL = url.toString();
|
|
|
String res = SendGET(finalURL, "");
|
|
|
JSONObject json = JSONObject.parseObject(res);
|
|
|
System.out.println(json.toString());
|
|
|
int errcode = json.getIntValue("errcode");
|
|
|
PrintWriter out = response.getWriter();
|
|
|
JSONObject toxcx = new JSONObject();
|
|
|
if(errcode == 0) {
|
|
|
String openid = json.getString("openid");
|
|
|
String session_key = json.getString("session_key");
|
|
|
//插入数据库中还没做
|
|
|
|
|
|
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
Random random=new Random();
|
|
|
StringBuffer sb=new StringBuffer();
|
|
|
for(int i=0;i<20;i++){
|
|
|
int number=random.nextInt(62);
|
|
|
sb.append(str.charAt(number));
|
|
|
}
|
|
|
toxcx.put("errcode","成功");
|
|
|
toxcx.put("verification",sb.toString());
|
|
|
out.println(toxcx.toString());
|
|
|
out.flush();
|
|
|
}
|
|
|
else if(errcode == -1) { // 系统繁忙,此时请开发者稍候再试
|
|
|
toxcx.put("errcode","-1 系统繁忙,请稍候再试");
|
|
|
out.println(toxcx.toString());
|
|
|
out.flush();
|
|
|
}
|
|
|
else if(errcode == 40029) { // code 无效
|
|
|
toxcx.put("errcode","40029 code无效");
|
|
|
out.println(toxcx.toString());
|
|
|
out.flush();
|
|
|
}
|
|
|
else if(errcode == 45011){ //频率限制,每个用户每分钟100次
|
|
|
toxcx.put("errcode","45011 操作过于频繁");
|
|
|
out.println(toxcx.toString());
|
|
|
out.flush();
|
|
|
}
|
|
|
}
|
|
|
}
|