Senin, 03 Januari 2011

Baca mutasi Bank Mandiri

public void readBankMandiri() throws Exception {
  
  HttpGet httpGet;
  HttpPost httpPost;
  HttpResponse resp;
  List<NameValuePair> formParams;
  UrlEncodedFormEntity entity;
  
  // open login page
  httpGet = new HttpGet("https://ib.bankmandiri.co.id/retail/Login.do?action=form&lang=in_ID");
  resp = httpClient.execute(httpGet); 
  EntityUtils.consume(resp.getEntity());
  
  // login
  formParams = new ArrayList<NameValuePair>();
  formParams.add(new BasicNameValuePair("action", "result"));
  formParams.add(new BasicNameValuePair("userID", "budi_handuk888"));
  formParams.add(new BasicNameValuePair("password", "123456"));
  formParams.add(new BasicNameValuePair("image.x", "0"));
  formParams.add(new BasicNameValuePair("image.y", "0"));
  entity = new UrlEncodedFormEntity(formParams, "UTF-8");
  httpPost = new HttpPost("https://ib.bankmandiri.co.id/retail/Login.do");
  httpPost.setEntity(entity);
  resp = httpClient.execute(httpPost);
  EntityUtils.consume(resp.getEntity());
  
  // check if login success?
  String successUrl = "https://ib.bankmandiri.co.id/retail/Redirect.do?action=forward";
  Header[] locationHeaders = resp.getHeaders("Location");
  if (locationHeaders.length == 0 || locationHeaders[0].getValue().equals(successUrl) == false) {
   System.out.println("Login failed!");
   return;
  }
  
  // open page to obtain account list
  httpGet = new HttpGet("https://ib.bankmandiri.co.id/retail/TrxHistoryInq.do?action=form");
  resp = httpClient.execute(httpGet);
  
  // read content
  StringBuilder buffer = new StringBuilder();
  InputStream instream = resp.getEntity().getContent();
  BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
  String line;
  while ((line = reader.readLine()) != null) {
   buffer.append(line);
  }
  
  // find string between: <select name="fromAccountID"> and </select>
  String begin = "<select name=\"fromAccountID\">", 
         end = "</select>";
  int beginPos = buffer.indexOf(begin) + begin.length(), 
      endPos = buffer.indexOf(end, beginPos);
  String accountStr = buffer.substring(beginPos, endPos);
  // account values is in each of <option value="{acc_id}">{acc_no}</option>
  ArrayList<String> accountList = new ArrayList<String>();
  begin = "<option value=\"";
  end = "\"";
  beginPos = 0;
  endPos = 0;
  while (true) {
   if ( (beginPos = accountStr.indexOf(begin, endPos)) == -1) break;
   beginPos += begin.length();
   if ( (endPos = accountStr.indexOf(end, beginPos)) == -1) break;
   // first account id will empty string "Silahkan Pilih"
   if (beginPos != endPos) {
    String accId = accountStr.substring(beginPos, endPos);
    accountList.add(accId);
   }
  }
  
  // get account statement
  formParams = new ArrayList<NameValuePair>();
  formParams.add(new BasicNameValuePair("action", "result"));
  formParams.add(new BasicNameValuePair("fromAccountID", accountList.get(0))); // <--- baca account pertama saja
  formParams.add(new BasicNameValuePair("searchType", "R"));
  formParams.add(new BasicNameValuePair("fromDay", "3")); // <--- tanggal from & to, cukup jelas ya
  formParams.add(new BasicNameValuePair("fromMonth", "12"));
  formParams.add(new BasicNameValuePair("fromYear", "2010"));
  formParams.add(new BasicNameValuePair("toDay", "3"));
  formParams.add(new BasicNameValuePair("toMonth", "1"));
  formParams.add(new BasicNameValuePair("toYear", "2011"));
  formParams.add(new BasicNameValuePair("sortType", "Date"));
  formParams.add(new BasicNameValuePair("orderBy", "ASC"));
  entity = new UrlEncodedFormEntity(formParams, "UTF-8");
  httpPost = new HttpPost("https://ib.bankmandiri.co.id/retail/TrxHistoryInq.do");
  httpPost.setEntity(entity);
  resp = httpClient.execute(httpPost);
  
  // read content
  buffer = new StringBuilder();
  instream = resp.getEntity().getContent();
  reader = new BufferedReader(new InputStreamReader(instream));
  while ((line = reader.readLine()) != null) {
   buffer.append(line + "\n");
  }
  
  //find string between: <!-- Start of Item List --> and <!-- End of Item List -->
  begin = "<!-- Start of Item List -->"; 
  end = "<!-- End of Item List -->";
  beginPos = buffer.indexOf(begin) + begin.length();
  endPos = buffer.indexOf(end, beginPos);
  String accountStmt = buffer.substring(beginPos, endPos);
  System.out.println(accountStmt);     
  
  // logout
  httpGet = new HttpGet("https://ib.bankmandiri.co.id/retail/Logout.do?action=result");
  resp = httpClient.execute(httpGet);  
  EntityUtils.consume(resp.getEntity());
  
 }

1 komentar: