1、准備工作
1)申請測試賬號,並接入自已的servlet
@WebServlet("/wx_token")
public class wx_token extends HttpServlet {
private static final long serialVersionUID = 1L;
public String token = "xxx";
public wx_token() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
System.out.println("signature:" + signature);
System.out.println("timestamp:" + timestamp);
System.out.println("nonce:" + nonce);
System.out.println("echostr:" + echostr);
PrintWriter pw = response.getWriter();
pw.append(echostr);
pw.flush();
}
}
servlet中的token跟這個配制一制即可,如果是xxx,那大家都是xxx.
下載natapp
將本地地址轉到外網地址
如果是tomcat,將域名直接訪問
通過以上的訪問,可以直接通過localhost訪問到ckySvr站
打開natapp.exe,可以看到http://開頭的域名,轉到了本地80端口,
打開微信測試賬號管理頁,找到
這樣微信中就配好了。
下面是實際的Servlet,這個Servlet主要是獲取code
@WebServlet("/OpenIdServlet")
public class OpenIdServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String openidURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
private String appId = "您的appId";
private String redirectURL = "http://10ec6069.ngrok.natapp.cn/OpenIdResultServlet";
public OpenIdServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String url = String.format(openidURL, appId, redirectURL);
response.sendRedirect(url);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
@WebServlet("/OpenIdResultServlet")
public class OpenIdResultServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String appId = "您的appId";
private String appSecret = "您的appSecret";
/**
* @see HttpServlet#HttpServlet()
*/
public OpenIdResultServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code");
String url = "https://api.weixin.qq.com/sns/oauth2/access_token";
String params = "appid=" + appId + "&secret=" + appSecret + "&code="
+ code + "&grant_type=authorization_code";
// request.setAttribute("code", url + "?" + params);
String s = HttpRequest.sendGet(url, params);
request.setAttribute("code", s);
try {
JSONObject jsonObject = new JSONObject(s);
String openid = jsonObject.getString("openid");
request.setAttribute("code", "openid:" + openid);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.getRequestDispatcher("result.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
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;
public class HttpRequest {
/**
* 向指定URL發送GET方法的請求
*
* @param url
* 發送請求的URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return URL 所代表遠程資源的響應結果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打開和URL之間的連接
URLConnection 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();
// 遍歷所有的響應頭字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定義 BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送GET請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 發送POST方法的請求
*
* @param url
* 發送請求的 URL
* @param param
* 請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
* @return 所代表遠程資源的響應結果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打開和URL之間的連接
URLConnection conn = realUrl.openConnection();
// 設置通用的請求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 發送POST請求必須設置如下兩行
conn.setDoOutput(true);
conn.setDoInput(true);
// 獲取URLConnection對象對應的輸出流
out = new PrintWriter(conn.getOutputStream());
// 發送請求參數
out.print(param);
// flush輸出流的緩沖
out.flush();
// 定義BufferedReader輸入流來讀取URL的響應
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("發送 POST 請求出現異常!" + e);
e.printStackTrace();
}
// 使用finally塊來關閉輸出流、輸入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}
這樣openid就輸出在了頁面上了。即加密的微信號
注意:測試賬號,是要關注了才可以獲取的到,認證的賬號應該不要,具體的要測試。
具體請參考微信官網說明:
下面就是實際測試了,這些離開了微信客戶端就沒有意義了。
所以打開掃一掃,掃一下我們初始的Servlet地址對應的URL
這樣你就可以在微信客戶端里看到了openid,即加密的微信號
