Advertisement
mendigo

Untitled

Sep 5th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package br.com.Controle;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.net.URLEncoder;
  11. import java.util.Map;
  12.  
  13.  
  14. public class Controle {
  15.    
  16.     private String login;
  17.     private String senha;
  18.     private String url;
  19.     private String cookie;
  20.     private String urlFicha;
  21.    
  22.     public Controle(String urlLogin, String urlFicha) {
  23.         this.url   = urlLogin;
  24.         this.urlFicha = urlFicha;
  25.     }
  26.     public void conectar(String login,String senha) throws Exception {
  27.         System.out.print("Conectando ................................. ");
  28.         try {
  29.             this.login = login;
  30.             this.senha = senha;
  31.             String  data  = URLEncoder.encode("login", "UTF-8")+"="+URLEncoder.encode(this.login, "UTF-8")+"&";
  32.                     data += URLEncoder.encode("senha", "UTF-8")+"="+URLEncoder.encode(this.senha, "UTF-8")+"&";
  33.                     data += URLEncoder.encode("submit", "UTF-8")+"="+URLEncoder.encode("Login", "UTF-8");
  34.            
  35.             URL url = new URL(this.url);
  36.            
  37.             // Send the request
  38.             URLConnection conn = url.openConnection();
  39.             conn.setDoOutput(true);
  40.             OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  41.                
  42.             //write parameters
  43.             writer.write(data);
  44.             writer.flush();
  45.            
  46.             // Get the response
  47.             StringBuffer answer = new StringBuffer();
  48.             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  49.             String line;
  50.             while ((line = reader.readLine()) != null) {
  51.                 answer.append(line);
  52.             }
  53.             writer.close();
  54.             reader.close();
  55.            
  56.             this.cookie = getSet_Cookie(conn.getHeaderFields());  
  57.              
  58.             //Output the response
  59.             System.out.println(answer.toString());
  60.            
  61.         } catch (MalformedURLException ex) {
  62.             ex.printStackTrace();
  63.         } catch (IOException ex) {
  64.             ex.printStackTrace();
  65.         } catch (Exception e) {
  66.             throw new Exception("Erro ao pegar sessao.");
  67.         }
  68.         System.out.println("[OK]");        
  69.     }
  70.    
  71.      public String getSet_Cookie(Map headers) {  
  72.        try{  
  73.           return  headers.get("Set-Cookie").toString().replace("[","").replace("]","").trim();  
  74.        }catch (Exception e) {   }
  75.        return this.cookie;  
  76.     }  
  77.    
  78.    
  79.     public String pegaFicha(String id) throws Exception {
  80.         System.out.print("Listando ficha ............................. ");
  81.         try {
  82.             URL url = new URL(this.urlFicha+"id");
  83.             URLConnection conn = url.openConnection();
  84.            
  85.             conn.setRequestProperty("Set-Cookie",this.cookie);  
  86.             conn.setRequestProperty("Set-Cookies",this.cookie);  
  87.             conn.setRequestProperty("Cookies",this.cookie);
  88.             conn.setRequestProperty("Cookie",this.cookie);
  89.            
  90.             // Get the response
  91.             StringBuffer answer = new StringBuffer();
  92.             BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  93.             String line;
  94.             while ((line = reader.readLine()) != null) {
  95.                 answer.append(line);
  96.             }
  97.             reader.close();
  98.            
  99.             System.out.println("[OK] <= id: "+id);
  100.             return answer.toString();
  101.         } catch (Exception e) {
  102.             System.out.println("[FALHOU] <= id: "+id);
  103.             e.printStackTrace();
  104.             throw new Exception("Erro ao pegar ficha!");
  105.         }
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement