Advertisement
mendigo

Untitled

Sep 2nd, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package br.com.Controle;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.net.HttpURLConnection;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.net.URLEncoder;
  11.  
  12. public class Controle {
  13.    
  14.     private String login;
  15.     private String senha;
  16.     private String url;
  17.     private String sessionCookie="";
  18.     private String urlFicha;
  19.     private HttpURLConnection urlConnection;
  20.    
  21.     public Controle(String urlLogin, String urlFicha) {
  22.         this.url   = urlLogin;
  23.         this.urlFicha = urlFicha;
  24.     }
  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;
  32.             data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(this.login, "UTF-8")+"&";
  33.             data += URLEncoder.encode("senha", "UTF-8") + "=" + URLEncoder.encode(this.senha, "UTF-8");
  34.            
  35.             URL url = new URL(this.url);  
  36.             this.urlConnection = (HttpURLConnection) url.openConnection();
  37.             this.urlConnection.setInstanceFollowRedirects(false);  
  38.             this.urlConnection.setDoOutput(true);
  39.             OutputStreamWriter wr = new OutputStreamWriter(this.urlConnection.getOutputStream());
  40.             wr.write(data);
  41.             wr.flush();
  42.             String headerName=null;
  43.             for (int i=1; (headerName = this.urlConnection.getHeaderFieldKey(i))!=null; i++) {  
  44.                 if ( headerName.equalsIgnoreCase("Set-Cookie") ) {                    
  45.                     if(sessionCookie == "") {  
  46.                         this.sessionCookie = this.urlConnection.getHeaderField(i);  
  47.                     }else {  
  48.                         this.sessionCookie += "; " + this.urlConnection.getHeaderField(i);  
  49.                     }
  50.                 }
  51.             }
  52.             System.out.println("[OK]");
  53.         } catch (MalformedURLException e) {
  54.             System.out.println("[FALHOU]");
  55.             System.out.println(e.getMessage());
  56.             throw new Exception("Erro ao pegar sessao.");
  57.         }  
  58.     }
  59.    
  60.     public String pegaFicha(String id) throws Exception {
  61.         System.out.print("Listando ficha ............................. ");
  62.         try {
  63.             //System.out.println(this.urlFicha+id);
  64.             URL urlSouceCode = new URL(this.urlFicha+id);  
  65.             this.urlConnection = (HttpURLConnection) urlSouceCode.openConnection();
  66.             this.urlConnection.setInstanceFollowRedirects(false);
  67.             this.urlConnection.setRequestProperty("Cookie", this.sessionCookie);  
  68.             InputStream is = this.urlConnection.getInputStream();  
  69.             InputStreamReader isr = new InputStreamReader(is);  
  70.             BufferedReader br = new BufferedReader(isr);  
  71.             String linha, tmp = "";
  72.             while ((linha = br.readLine())!= null) {
  73.                 tmp += linha;
  74.                 linha = br.readLine();
  75.             }
  76.             System.out.println("[OK] <= id: "+id);
  77.             return tmp;
  78.         } catch (Exception e) {
  79.             System.out.println("[FALHOU] <= id: "+id);
  80.             System.out.println(e.getMessage());
  81.             throw new Exception("Erro ao pegar ficha!");
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement