public static String fetchScreen(String username, String password) { String line = ""; try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {} } }; String url = "https://www2.virginmobileusa.com/login/login.do"; String dataUrl = "https://www2.virginmobileusa.com/myaccount/dataPlanHistory.do"; // String url = "https://www1.virginmobileusa.com/login/login.do"; // String url = "https://www1.virginmobileusa.com/myaccount/home.do"; try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.getMessage(); } // HttpsURLConnection.setFollowRedirects(true); HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier()); // connection.setFollowRedirects(true); connection.setDoOutput(true); String content = "loginRoutingInfo=&min=" + username + "&vkey=" + password + "&submit=submit"; connection.setFixedLengthStreamingMode(content.length()); connection.setRequestProperty("Host", "www2.virginmobileusa.com"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.9"); // try { // Thread.sleep(5000); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(content); out.close(); // } catch (IOException e) { // e.printStackTrace(); // } // connection.connect(); InputStreamReader in = new InputStreamReader((InputStream) connection.getContent()); BufferedReader buff = new BufferedReader(in); StringBuilder sb = new StringBuilder(); while ((line = buff.readLine()) != null) { sb.append(line); } int mainContentIndex = sb.indexOf("id=\"mainContent\""); if (mainContentIndex == -1) { line = ""; } else { line = sb.substring(mainContentIndex); } // Now, try to grab data usage String cookies = ""; // 1. Grab and store cookies String headerName = null; for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) { if (headerName.equalsIgnoreCase("Set-Cookie")) { String cookie = connection.getHeaderField(i); cookie = cookie.substring(0, cookie.indexOf(";")); String cookieName = cookie.substring(0, cookie.indexOf("=")); String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); cookies = cookies + cookieName + "=" + cookieValue + "; "; } } // connection.disconnect(); // 2. Grab the next page BufferedReader buff2 = new BufferedReader(in); StringBuilder sb2 = new StringBuilder(); connection = (HttpsURLConnection) new URL(dataUrl).openConnection(); ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier()); // connection.setFollowRedirects(true); connection.setDoOutput(true); connection.setRequestProperty("Cookie", cookies); // connection.connect(); in = new InputStreamReader((InputStream) connection.getContent()); buff2 = new BufferedReader(in); sb2 = new StringBuilder(); String dataPage; while ((dataPage = buff2.readLine()) != null) { sb2.append(dataPage); } int dataContentIndex = sb2.indexOf("<h2>Mobile Web History</h2>"); if (dataContentIndex == -1) { dataPage = ""; } else { dataPage = sb2.substring(dataContentIndex); } // Simply concat the output with our data page output if (line != null) { // line.concat("---------------TEST----------------------"+ dataPage); // line = dataPage; } } catch (Exception e) { e.printStackTrace(); // System.err.println("exception 83"); // System.err.println(e.getMessage()); // System.err.println(line); return line; // rc.put("isValid", "FALSE"); } // line = null; if (line == null) { // line = ""; } // System.err.println(line); return line; }
// gets VM home and data page based on boolean parameter public static String fetchURL(String username, String password, Boolean getData) { String line = ""; String url = "https://www2.virginmobileusa.com/login/login.do"; String dataUrl = "https://www2.virginmobileusa.com/myaccount/dataPlanHistory.do"; try { TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted( java.security.cert.X509Certificate[] certs, String authType) {} public void checkServerTrusted( java.security.cert.X509Certificate[] certs, String authType) {} } }; try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.getMessage(); } HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection(); ((HttpsURLConnection) connection).setHostnameVerifier(new AllowAllHostnameVerifier()); connection.setDoOutput(true); String content = "loginRoutingInfo=&min=" + username + "&vkey=" + password + "&submit=submit"; connection.setFixedLengthStreamingMode(content.length()); connection.setRequestProperty("Host", "www2.virginmobileusa.com"); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.9"); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); out.write(content); out.close(); InputStreamReader in = new InputStreamReader((InputStream) connection.getContent()); BufferedReader buff = new BufferedReader(in); StringBuilder sb = new StringBuilder(); while ((line = buff.readLine()) != null) { sb.append(line); } // telephone int tel_infoStartIndex = sb.indexOf("id=\"headerimage\""); int tel_infoEndIndex = sb.indexOf("<ul class=\"tabnav\">"); // balance info int balance_infoStartIndex = sb.indexOf("id=\"balance_info2b\">"); int balance_infoEndIndex = sb.indexOf("id=\"payment_strip\""); // account activity int account_activityStartIndex = sb.indexOf("id=\"account_activity\""); int account_activityEndIndex = sb.indexOf("id=\"for_your_phone\""); // int mainContentIndex = sb.indexOf("id=\"mainContent\""); if (tel_infoStartIndex == -1) { line = ""; } else { line = sb.substring(tel_infoStartIndex, tel_infoEndIndex); } if (balance_infoStartIndex != -1) { line += sb.substring(balance_infoStartIndex, balance_infoEndIndex); } if (account_activityStartIndex != -1) { line += sb.substring(account_activityStartIndex, account_activityEndIndex); } // Now, try to grab data usage String cookies = ""; // 1. Grab and store cookies String headerName = null; for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) { if (headerName.equalsIgnoreCase("Set-Cookie")) { String cookie = connection.getHeaderField(i); cookie = cookie.substring(0, cookie.indexOf(";")); String cookieName = cookie.substring(0, cookie.indexOf("=")); String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); cookies = cookies + cookieName + "=" + cookieValue + "; "; } } if (getData == true) { // get data page HttpsURLConnection connection2 = (HttpsURLConnection) new URL(dataUrl).openConnection(); ((HttpsURLConnection) connection2).setHostnameVerifier(new AllowAllHostnameVerifier()); connection2.setDoOutput(true); connection2.setRequestProperty("Cookie", cookies); connection2.connect(); InputStreamReader in2 = new InputStreamReader((InputStream) connection2.getContent()); BufferedReader buff2 = new BufferedReader(in2); StringBuilder sb2 = new StringBuilder(); String dataPage; while ((dataPage = buff2.readLine()) != null) { sb2.append(dataPage); } int dataContentIndex = sb2.indexOf("<h2>Mobile Web History</h2>"); if (dataContentIndex == -1) { dataPage = ""; } else { dataPage = sb2.substring(dataContentIndex); line += ("****************" + dataPage); } } } catch (Exception e) { e.printStackTrace(); // System.err.println("exception 83"); // System.err.println(e.getMessage()); // System.err.println(line); return line; // rc.put("isValid", "FALSE"); } return line; }
// HTTP PUT change to the server // Return 0 on success // Return 1 on Android failure // Return 2 on server failure private void updateItem() { URL url; HttpsURLConnection urlConnection = null; int size; byte[] data; OutputStream out; String itemUrl = UPDATE_ITEM_URL + "/" + mItem.getId(); try { url = new URL(itemUrl); urlConnection = (HttpsURLConnection) url.openConnection(); // Set authentication instance ID urlConnection.setRequestProperty( MyConstants.HTTP_HEADER_INSTANCE_ID, InstanceID.getInstance(getApplicationContext()).getId()); // Set content type urlConnection.setRequestProperty("Content-Type", "application/json"); // To upload data to a web server, configure the connection for output using // setDoOutput(true). It will use POST if setDoOutput(true) has been called. urlConnection.setDoOutput(true); urlConnection.setRequestMethod("PUT"); // Convert item to JSON string JSONObject jsonItem = new JSONObject(); JSONObject jsonMember = new JSONObject(); JSONArray jsonMembers = new JSONArray(); jsonMember.put("attendant", change); if (change >= 1 && mPhoneNumber != null && !mPhoneNumber.isEmpty()) { jsonMember.put("phonenumber", mPhoneNumber); } jsonMembers.put(jsonMember); jsonItem.put("members", jsonMembers); data = jsonItem.toString().getBytes(); // For best performance, you should call either setFixedLengthStreamingMode(int) when the // body length is known in advance, or setChunkedStreamingMode(int) when it is not. // Otherwise HttpURLConnection will be forced to buffer the complete request body in memory // before it is transmitted, wasting (and possibly exhausting) heap and increasing latency. size = data.length; if (size > 0) { urlConnection.setFixedLengthStreamingMode(size); } else { // Set default chunk size urlConnection.setChunkedStreamingMode(0); } // Get the OutputStream of HTTP client out = new BufferedOutputStream(urlConnection.getOutputStream()); // Copy from file to the HTTP client out.write(data); // Make sure to close streams, otherwise "unexpected end of stream" error will happen out.close(); // Check canceled if (isCancelled()) { Log.d(LOG_TAG, "Updating item canceled"); status = UpdateItemStatus.ANDROID_FAILURE; return; } // Set timeout urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); // Vernon debug Log.d( LOG_TAG, urlConnection.getRequestMethod() + " " + urlConnection.getURL().toString() + new String(data)); // Send and get response // getResponseCode() will automatically trigger connect() int responseCode = urlConnection.getResponseCode(); String responseMsg = urlConnection.getResponseMessage(); Log.d(LOG_TAG, "Response " + responseCode + " " + responseMsg); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { Log.d(LOG_TAG, "Server says the item was closed"); status = UpdateItemStatus.ITEM_CLOSED; return; } if (responseCode != HttpURLConnection.HTTP_OK) { Log.d(LOG_TAG, "Update item attendant " + change + " failed"); status = UpdateItemStatus.SERVER_FAILURE; return; } // Vernon debug Log.d(LOG_TAG, "Update item attendant " + change + " successfully"); } catch (Exception e) { e.printStackTrace(); Log.d(LOG_TAG, "Update item failed because " + e.getMessage()); status = UpdateItemStatus.ANDROID_FAILURE; } finally { if (urlConnection != null) { urlConnection.disconnect(); } } }