?? generatetoken.java
字號:
/* * GenerateToken.java * * Created on 22 Август 2006 г., 21:16 */package org.vorobev.mgtalk;import com.twmacinta.util.MD5;import java.io.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;/** * * @author kostya * @version */public class GenerateToken extends HttpServlet { private String readLine(DataInputStream dis) { String s = ""; byte ch = 0; try { while((ch = dis.readByte())!=-1) {// System.out.println("ch = "+ch); if(ch=='\n') return s; s += (char)ch; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return s; } protected String generateToken(String email, String pass) { try { String first = "Email="+email+"&Passwd="+pass+"&PersistentCookie=false&source=googletalk"; URL u = new URL("https://www.google.com:443/accounts/ClientAuth?"+first); HttpURLConnection huc = (HttpURLConnection)u.openConnection(); DataInputStream dis = new DataInputStream(huc.getInputStream()); String str = readLine(dis); String SID = ""; String LSID = ""; if(str.startsWith("SID=")) { SID = str.substring(4, str.length()); str = readLine(dis); LSID = str.substring(5, str.length()); first = "SID="+SID+"&LSID="+LSID+"&service=mail&Session=true"; dis.close(); u = new URL("https://www.google.com:443/accounts/IssueAuthToken?"+first); huc = (HttpURLConnection)u.openConnection(); dis = new DataInputStream(huc.getInputStream()); str = readLine(dis); String token = MD5.toBase64(new String("\0"+email+"\0"+str).getBytes()); dis.close(); return token; } } catch (Throwable ex) { ex.printStackTrace(); } return "tokenError"; } /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) { try { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(generateToken(request.getParameter("email"), request.getParameter("pass"))); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold>}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -