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.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- public class Controle {
- private String login;
- private String senha;
- private String url;
- private String sessionCookie="";
- private String urlFicha;
- private HttpURLConnection urlConnection;
- 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;
- data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(this.login, "UTF-8")+"&";
- data += URLEncoder.encode("senha", "UTF-8") + "=" + URLEncoder.encode(this.senha, "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();
- String headerName=null;
- for (int i=1; (headerName = this.urlConnection.getHeaderFieldKey(i))!=null; i++) {
- if ( headerName.equalsIgnoreCase("Set-Cookie") ) {
- if(sessionCookie == "") {
- this.sessionCookie = this.urlConnection.getHeaderField(i);
- }else {
- this.sessionCookie += "; " + this.urlConnection.getHeaderField(i);
- }
- }
- }
- 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 {
- //System.out.println(this.urlFicha+id);
- URL urlSouceCode = new URL(this.urlFicha+id);
- this.urlConnection = (HttpURLConnection) urlSouceCode.openConnection();
- this.urlConnection.setInstanceFollowRedirects(false);
- this.urlConnection.setRequestProperty("Cookie", this.sessionCookie);
- InputStream is = this.urlConnection.getInputStream();
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String linha, tmp = "";
- while ((linha = br.readLine())!= 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