jeudi 12 mars 2015

Using cookies with a URLConnection (Java/Eclipse)



I have been working on this method for eight consecutive hours, and really need some help.


I am currently trying to simulate a form login and receive the cookie in return (complete). However, the second step which involves using the login cookies to access other parts of the site is seemingly impossible. As a note, I am newer to Java, so bear with me :/


public class mainClass {



public static void main(String[] args) throws Exception{
sources vars = new sources();
int passint = 0,userint = 0;
String password=null,username=null,layoutte=vars.Lay;
URLConnection oracle = new URL(vars.hComputer).openConnection();
JPanel userPanel = new JPanel(); userPanel.setLayout(new GridLayout(2,2));
JLabel usernameLbl = new JLabel("Username:");
JLabel passwordLbl = new JLabel("Password:");
JTextField usename = new JTextField();
JPasswordField passwordFld = new JPasswordField();
userPanel.add(usernameLbl);
userPanel.add(usename);
userPanel.add(passwordLbl);
userPanel.add(passwordFld);
int input = JOptionPane.showConfirmDialog(null, userPanel, "Enter your information:" ,JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);

char[] pass = passwordFld.getPassword();
vars.Pass = new String(pass);
vars.Login = usename.getText();
password = vars.Pass;
username = vars.Login;

CookieManager manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);


URL url = new URL(vars.passAuth);
URLConnection con = url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36");
con.setDoOutput(true);


PrintWriter wr = new PrintWriter(con.getOutputStream(), true);
StringBuilder parameters = new StringBuilder();
parameters.append("login=" + URLEncoder.encode(username, "UTF-8"));
parameters.append("&");
parameters.append("lpass=" + URLEncoder.encode(password, "UTF-8"));
parameters.append("&");
parameters.append("layout=" + URLEncoder.encode(layoutte, "UTF-8"));
wr.println(parameters);
System.out.println(parameters);
wr.close();

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
for (int i=1; (line = con.getHeaderFieldKey(i))!=null; i++) {
if (line.equals("Set-Cookie")) {
String cookie = con.getHeaderField(i);

try{
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") +1, cookie.length());


}catch (Exception e){
System.out.println("Not a cookie!");
}



CookieStore cookieJar = manager.getCookieStore();
List <HttpCookie> cookies =
cookieJar.getCookies();
for (HttpCookie cookiee: cookies) {

oracle.setRequestProperty("Cookie", cookiee.toString());;

}

}//cookies

else if (line == null)
break;
}
br.close();

try{
oracle.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
oracle.connect();


BufferedReader reader2 = new BufferedReader(new InputStreamReader(oracle.getInputStream(), Charset.forName("UTF-8")));

StringBuilder sb = new StringBuilder();
String leene;
while ((leene = reader2.readLine()) != null) {
sb.append(leene);
}
System.out.println(sb.toString());


}catch (IOException e){
System.out.println(e+"BAD");
}

}

}//pub class



Aucun commentaire:

Enregistrer un commentaire