Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.Controle;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.Map;
- public class Controle {
- private String login;
- private String senha;
- private String url;
- private String cookie;
- private String urlFicha;
- public Controle(String urlLogin, String urlFicha) {
- this.url = urlLogin;
- this.urlFicha = urlFicha;
- }
- public void conectar(String login,String senha) throws Exception {
- System.out.print("Conectando ................................. ");
- try {
- this.login = login;
- this.senha = senha;
- String data = URLEncoder.encode("login", "UTF-8")+"="+URLEncoder.encode(this.login, "UTF-8")+"&";
- data += URLEncoder.encode("senha", "UTF-8")+"="+URLEncoder.encode(this.senha, "UTF-8")+"&";
- data += URLEncoder.encode("submit", "UTF-8")+"="+URLEncoder.encode("Login", "UTF-8");
- URL url = new URL(this.url);
- // Send the request
- URLConnection conn = url.openConnection();
- conn.setDoOutput(true);
- OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
- //write parameters
- writer.write(data);
- writer.flush();
- // Get the response
- StringBuffer answer = new StringBuffer();
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = reader.readLine()) != null) {
- answer.append(line);
- }
- writer.close();
- reader.close();
- this.cookie = getSet_Cookie(conn.getHeaderFields());
- //Output the response
- System.out.println(answer.toString());
- } catch (MalformedURLException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (Exception e) {
- throw new Exception("Erro ao pegar sessao.");
- }
- System.out.println("[OK]");
- }
- public String getSet_Cookie(Map headers) {
- try{
- return headers.get("Set-Cookie").toString().replace("[","").replace("]","").trim();
- }catch (Exception e) { }
- return this.cookie;
- }
- public String pegaFicha(String id) throws Exception {
- System.out.print("Listando ficha ............................. ");
- try {
- URL url = new URL(this.urlFicha+"id");
- URLConnection conn = url.openConnection();
- conn.setRequestProperty("Set-Cookie",this.cookie);
- conn.setRequestProperty("Set-Cookies",this.cookie);
- conn.setRequestProperty("Cookies",this.cookie);
- conn.setRequestProperty("Cookie",this.cookie);
- // Get the response
- StringBuffer answer = new StringBuffer();
- BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String line;
- while ((line = reader.readLine()) != null) {
- answer.append(line);
- }
- reader.close();
- System.out.println("[OK] <= id: "+id);
- return answer.toString();
- } catch (Exception e) {
- System.out.println("[FALHOU] <= id: "+id);
- e.printStackTrace();
- throw new Exception("Erro ao pegar ficha!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement