Advertisement
mendigo

Untitled

Sep 2nd, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 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.OutputStream;
  7. import java.io.OutputStreamWriter;
  8. import java.net.HttpURLConnection;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11. import java.net.URLEncoder;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. import br.com.Objeto.Cookie;
  16. import br.com.Ver.CookieManager;
  17.  
  18. public class Controle {
  19.    
  20.     private String login;
  21.     private String senha;
  22.     private String url;
  23.     private List<Cookie> sessionCookie;
  24.     private String urlFicha;
  25.     private HttpURLConnection urlConnection;
  26.    
  27.     public Controle(String urlLogin, String urlFicha) {
  28.         this.url   = urlLogin;
  29.         this.urlFicha = urlFicha;
  30.         this.sessionCookie = new ArrayList<Cookie>();
  31.     }
  32.    
  33.    
  34.     public void con(String login, String senha) throws Exception {
  35.        
  36.        
  37.     }
  38.    
  39.    
  40.     public void conectar(String login,String senha) throws Exception {
  41.         System.out.print("Conectando ................................. ");
  42.         try {
  43.            
  44.             this.login = login;
  45.             this.senha = senha;
  46.             String data;
  47.             data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(this.login, "UTF-8")+"&";
  48.             data += URLEncoder.encode("senha", "UTF-8") + "=" + URLEncoder.encode(this.senha, "UTF-8")+"&";
  49.             data += URLEncoder.encode("submit", "UTF-8") + "=" + URLEncoder.encode("Login", "UTF-8");
  50.            
  51.             URL url = new URL(this.url);
  52.             this.urlConnection = (HttpURLConnection) url.openConnection();
  53.             this.urlConnection.setInstanceFollowRedirects(false);  
  54.             this.urlConnection.setDoOutput(true);
  55.             OutputStreamWriter wr = new OutputStreamWriter(this.urlConnection.getOutputStream());
  56.             wr.write(data);
  57.             wr.flush();
  58.            
  59.             CookieManager ck = new CookieManager();
  60.             String headerName=null, cookie = "";
  61.             for (int i=1; (headerName = this.urlConnection.getHeaderFieldKey(i))!=null; i++) {
  62.                 if ( headerName.equalsIgnoreCase("Set-Cookie") ) {                    
  63.                     cookie = this.urlConnection.getHeaderField(i);
  64.                     for (int j = 0; j < cookie.split(";").length; j++) {
  65.                         sessionCookie.add(new Cookie(cookie.split(";")[j]));
  66.                     }
  67.                 }
  68.             }
  69.             System.out.println("[OK]");
  70.         } catch (MalformedURLException e) {
  71.             System.out.println("[FALHOU]");
  72.             System.out.println(e.getMessage());
  73.             throw new Exception("Erro ao pegar sessao.");
  74.         }  
  75.     }
  76.     public String pegaFicha(String id) throws Exception {
  77.         System.out.print("Listando ficha ............................. ");
  78.         try {
  79.             URL urlSouceCode = new URL(this.urlFicha+id);  
  80.             this.urlConnection.setInstanceFollowRedirects(false);
  81.             this.urlConnection = (HttpURLConnection) urlSouceCode.openConnection();
  82.            
  83.             String cke = "";
  84.             for (Cookie cookie : sessionCookie) {
  85.                 cke += cookie.getNome()+"="+cookie.getValor()+"; ";
  86.             }
  87.             this.urlConnection.setRequestProperty("Cookie", cke.substring(0,(cke.length()-2)));
  88.             InputStream is = this.urlConnection.getInputStream();  
  89.             InputStreamReader isr = new InputStreamReader(is);  
  90.             BufferedReader br = new BufferedReader(isr);  
  91.             System.out.println(br.readLine());
  92.             String linha = br.readLine(), tmp = "";
  93.             while (linha != null) {
  94.                 tmp += linha;
  95.                 linha = br.readLine();
  96.             }
  97.             System.out.println("[OK] <= id: "+id);
  98.             return tmp;
  99.         } catch (Exception e) {
  100.             System.out.println("[FALHOU] <= id: "+id);
  101.             System.out.println(e.getMessage());
  102.             throw new Exception("Erro ao pegar ficha!");
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement