private void bootstrap() { this.builder = new RequestBuilder(); this.handler = new ResponseHandler(); this.navigator = new Navigator(this.builder, this.handler); builder.setHost(navigator.getHost().getHost()); List basic = this.navigator.basicRequest(navigator.getHost().toExternalForm()); if (handler.isRedirect(basic)) { String location = handler.getRedirectedLocation(basic); builder.addHeader("Referer", location); try { URL url = new URL(location); navigator.setHost(url); builder.setHost(url.getHost()); } catch (Exception e) { e.printStackTrace(); } List redirect = navigator.basicRequest(location + "content/prehome.jsp"); String cookie = handler.extractCookie(redirect); builder.addHeader("Cookie", cookie); } }
/** @return Boolean */ public boolean logout() { builder.addHeader( "Referer", navigator.getHost().toExternalForm() + "Main.action?id=" + builder.getToken()); navigator.basicRequest( navigator.getHost().toExternalForm() + "entry.jsp?h=6WP0NWtKYIcKKIWjX7XIwyppK3gftCXI"); return true; }
/** @return Boolean */ public boolean login() { if (this.account == null) return false; builder.addHeader("Referer", navigator.getHost().toExternalForm() + "content/index.html"); Map post = new HashMap<String, String>(); post.put("username", this.account.getUserName()); post.put("password", this.account.getPassword()); post.put("userLogin", "no"); post.put("button", "Login"); List result = navigator.basicPost(navigator.getHost().toExternalForm() + "w2sauth.action", post); if (handler.isRedirect(result)) { String token = handler.extractToken(result); builder.setToken(token); navigator.followRedirect(result); navigator.setLoggedIn(true); return true; } else return false; }
/** * @param mobileNumber String * @param message String * @return Boolean */ public boolean sendMessage(String mobileNumber, String message) { if (!navigator.isLoggedIn()) return false; if (message.length() > 140) return false; builder.addHeader( "Referer", navigator.getHost().toExternalForm() + "Main.action?id=" + builder.getToken()); List sms = navigator.basicRequest( navigator.getHost().toExternalForm() + "jsp/SingleSMS.jsp?Token=" + builder.getToken()); builder.addHeader( "Referer", navigator.getHost().toExternalForm() + "jsp/SingleSMS.jsp?Token=" + builder.getToken()); String m_15_b = this.findHtml(sms, "m_15_b"); String t_15_k_5 = this.findHtml(sms, "t_15_k_5"); String diffNo = this.findHtml(sms, "diffNo"); try { message = URLEncoder.encode(message, "utf-8"); diffNo = URLEncoder.encode(diffNo, "utf-8"); } catch (Exception e) { e.printStackTrace(); } String nonce = ".setAttribute(\"name\","; for (Iterator i = sms.iterator(); i.hasNext(); ) { String line = (String) i.next(); if (line.indexOf(nonce) > 0) { line = line.substring(line.indexOf(nonce) + nonce.length(), line.indexOf(')')).trim(); nonce = line.substring(1, line.length() - 1); break; } } String cookie = "setCookie(\""; for (Iterator i = sms.iterator(); i.hasNext(); ) { String line = (String) i.next(); int index = line.indexOf(cookie); if (index > 0) { line = line.substring(index + cookie.length()); cookie = line.substring(0, line.indexOf('"')); break; } } if (!cookie.isEmpty()) builder.addHeader("Cookie", builder.getHeader("Cookie") + "; " + cookie + "=" + cookie); Map post = new HashMap<String, String>(); post.put("m_15_b", m_15_b); post.put(m_15_b, mobileNumber); post.put("t_15_k_5", t_15_k_5); post.put(t_15_k_5, builder.getToken()); post.put("i_m", "sndsms"); post.put("txtLen", 140 - message.length()); post.put("textArea", message); post.put("kriya", this.findHtml(sms, "kriya")); post.put("chkall", "on"); post.put("diffNo", diffNo); post.put(nonce, ""); post.put("catnamedis", "Birthday"); List sent = navigator.basicPost(navigator.getHost().toExternalForm() + "jsp/m2msms.action", post); if (handler.isRedirect(sent)) navigator.followRedirect(sent); return true; }