Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.MalformedURLException;
  8. import java.net.ProtocolException;
  9. import java.net.URL;
  10. import java.util.stream.IntStream;
  11.  
  12. import javax.net.ssl.HttpsURLConnection;
  13.  
  14. public class Crawler {
  15.  
  16. private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
  17.  
  18. private final String user;
  19. private final String password;
  20. private final TipoLogin tipo;
  21.  
  22. public static enum TipoLogin {
  23. CONTRIBUINTE_ICMS("rdBtnContribuinte"),
  24. CONSUMIDOR("rdBtnNaoContribuinte"),
  25. CONTABILISTA("rdBtnContabilista"),
  26. FAZENDARIO("rdBtnFazendario"),
  27. PROCON("rdBtnProcon"),
  28. REPRESENTANTE_CONTRIBUINTE("rdBtnAdvogadoRepresentante");
  29.  
  30. private final String radio;
  31.  
  32. private TipoLogin(String radio) {
  33. this.radio = radio;
  34. }
  35.  
  36. public String getRadio() {
  37. return radio;
  38. }
  39. }
  40.  
  41. public static void main(String[] args) throws IOException {
  42. Crawler http = new Crawler("12345678901", "$enh4", TipoLogin.CONTRIBUINTE_ICMS);
  43. http.sendPost();
  44. }
  45.  
  46. public Crawler(String user, String password, TipoLogin tipo) {
  47. this.user = user;
  48. this.password = password;
  49. this.tipo = tipo;
  50. }
  51.  
  52. // HTTP POST request
  53. private void sendPost() throws IOException {
  54. URL url;
  55. try {
  56. url = new URL("https://www.nfp.fazenda.sp.gov.br/login.aspx");
  57. } catch (MalformedURLException e) {
  58. throw new AssertionError(e);
  59. }
  60.  
  61. HttpsURLConnection get = (HttpsURLConnection) url.openConnection();
  62. get.setRequestProperty("User-Agent", USER_AGENT);
  63. get.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  64. get.getResponseCode();
  65. String page = download(get.getInputStream());
  66.  
  67. HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
  68.  
  69. try {
  70. con.setRequestMethod("POST");
  71. } catch (ProtocolException e) {
  72. throw new AssertionError(e);
  73. }
  74. con.setRequestProperty("User-Agent", USER_AGENT);
  75. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  76.  
  77. String urlParameters = "__EVENTTARGET=" + buscarCampo(page, "__EVENTTARGET")
  78. + "&__EVENTARGUMENT=" + buscarCampo(page, "__EVENTARGUMENT")
  79. + "&__VIEWSTATE=" + buscarCampo(page, "__VIEWSTATE")
  80. + "&__EVENTVALIDATION=" + buscarCampo(page, "__EVENTVALIDATION")
  81. + "&ctl00$ddlTipoUsuario=#rdBtnNaoContribuinte"
  82. + "&ctl00$UserNameAcessivel="
  83. + "&ctl00$PasswordAcessivel="
  84. + "&ctl00$ConteudoPagina$Login1$rblTipo=" + tipo.getRadio()
  85. + "&ctl00$ConteudoPagina$Login1$UserName=" + escapeURI(user)
  86. + "&ctl00$ConteudoPagina$Login1$Password=" + escapeURI(password);
  87.  
  88. System.out.println("Parâmetros parameters : " + urlParameters);
  89.  
  90. // Send post request
  91. con.setDoOutput(true);
  92. try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
  93. wr.writeBytes(urlParameters);
  94. wr.flush();
  95. }
  96.  
  97. int responseCode = con.getResponseCode();
  98. System.out.println("Enviando 'POST' request para a URL : " + url);
  99. System.out.println("Response Code: " + responseCode);
  100.  
  101. String response = download(con.getInputStream());
  102.  
  103. //print result
  104. System.out.println(response);
  105. }
  106.  
  107. private static String download(InputStream is) throws IOException {
  108. StringBuilder response = new StringBuilder(1024);
  109. try (BufferedReader in = new BufferedReader(new InputStreamReader(is))) {
  110. String inputLine;
  111.  
  112. while ((inputLine = in.readLine()) != null) {
  113. response.append(inputLine);
  114. }
  115. }
  116. return response.toString();
  117. }
  118.  
  119. private static String buscarCampo(String html, String campo) {
  120. String input = "<input type="hidden" name="" + campo + "" id="" + campo + "" value="";
  121. int a = html.indexOf(input);
  122. if (a == -1) return "";
  123. int b = html.indexOf('"', a + input.length());
  124. return html.substring(a + input.length(), b);
  125. }
  126.  
  127. private static final String[] HEX = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};
  128.  
  129. private static String escapeURI(byte c) {
  130. boolean ok = (c >= 'A' && c <= 'Z')
  131. || (c >= 'a' && c <= 'z')
  132. || (c >= '0' && c <= '9')
  133. || c == '-' || c == '.' || c == '_' || c == '~'
  134. || c == '$' || c == '#';
  135. return ok ? String.valueOf((char) c) : "%" + HEX[c >>> 4] + HEX[c & 0xF];
  136. }
  137.  
  138. public static String escapeURI(String in) {
  139. StringBuilder sb = new StringBuilder(in.length() * 2);
  140. try {
  141. byte[] bytes = in.getBytes("UTF-8");
  142. IntStream.range(0, bytes.length).mapToObj(i -> escapeURI(bytes[i])).forEach(sb::append);
  143. } catch (UnsupportedEncodingException e) {
  144. throw new AssertionError(e);
  145. }
  146. return sb.toString();
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement