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.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.List;
- import br.com.Objeto.Cookie;
- import br.com.Ver.CookieManager;
- public class Controle {
- private String login;
- private String senha;
- private String url;
- private List<Cookie> sessionCookie;
- private String urlFicha;
- private HttpURLConnection urlConnection;
- public Controle(String urlLogin, String urlFicha) {
- this.url = urlLogin;
- this.urlFicha = urlFicha;
- this.sessionCookie = new ArrayList<Cookie>();
- }
- public void con(String login, String senha) throws Exception {
- }
- public void conectar(String login,String senha) throws Exception {
- System.out.print("Conectando ................................. ");
- try {
- this.login = login;
- this.senha = senha;
- String data;
- 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);
- this.urlConnection = (HttpURLConnection) url.openConnection();
- this.urlConnection.setInstanceFollowRedirects(false);
- this.urlConnection.setDoOutput(true);
- OutputStreamWriter wr = new OutputStreamWriter(this.urlConnection.getOutputStream());
- wr.write(data);
- wr.flush();
- CookieManager ck = new CookieManager();
- String headerName=null, cookie = "";
- for (int i=1; (headerName = this.urlConnection.getHeaderFieldKey(i))!=null; i++) {
- if ( headerName.equalsIgnoreCase("Set-Cookie") ) {
- cookie = this.urlConnection.getHeaderField(i);
- for (int j = 0; j < cookie.split(";").length; j++) {
- sessionCookie.add(new Cookie(cookie.split(";")[j]));
- }
- }
- }
- System.out.println("[OK]");
- } catch (MalformedURLException e) {
- System.out.println("[FALHOU]");
- System.out.println(e.getMessage());
- throw new Exception("Erro ao pegar sessao.");
- }
- }
- public String pegaFicha(String id) throws Exception {
- System.out.print("Listando ficha ............................. ");
- try {
- URL urlSouceCode = new URL(this.urlFicha+id);
- this.urlConnection.setInstanceFollowRedirects(false);
- this.urlConnection = (HttpURLConnection) urlSouceCode.openConnection();
- String cke = "";
- for (Cookie cookie : sessionCookie) {
- cke += cookie.getNome()+"="+cookie.getValor()+"; ";
- }
- this.urlConnection.setRequestProperty("Cookie", cke.substring(0,(cke.length()-2)));
- InputStream is = this.urlConnection.getInputStream();
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- System.out.println(br.readLine());
- String linha = br.readLine(), tmp = "";
- while (linha != null) {
- tmp += linha;
- linha = br.readLine();
- }
- System.out.println("[OK] <= id: "+id);
- return tmp;
- } catch (Exception e) {
- System.out.println("[FALHOU] <= id: "+id);
- System.out.println(e.getMessage());
- throw new Exception("Erro ao pegar ficha!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement