1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class cook {
public static void main(String args[]) throws IOException{
URL url = new URL("https://www.creditmutuel.fr/cmne/fr/identification/default.cgi" );
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
try {
// envoi POST
String cookie = uc.getHeaderField(8);
cookie = cookie.substring(0, cookie.indexOf(";" ));
System.out.println(cookie);
uc = (HttpsURLConnection) url.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("method","POST" );
uc.addRequestProperty("Cookie", cookie);
uc.addRequestProperty("Content-type","application/x-www-form-urlencoded" );
PrintWriter pout = new PrintWriter( new OutputStreamWriter(
uc.getOutputStream(), "iso-8859-1" ), true );
pout.print("_cm_user=0290012345678&_cm_pwd=87654321&_cm_app=SITFIN&_cm_langue=fr" );
pout.flush();
// lecture réponse
uc = (HttpsURLConnection) url.openConnection();
uc.setRequestProperty("Cookie", cookie);
File fichier = new File("/home/eric/Bureau/extrait" ) ;
BufferedWriter ecr = new BufferedWriter (new FileWriter (fichier));
BufferedReader lit =new BufferedReader(new InputStreamReader(uc.getInputStream()));
System.out.println ("début" );
if (! fichier.exists()) fichier.createNewFile();
do{
String ligne=lit.readLine();
if(ligne==null)break;
ecr.write(ligne);
} while(true);
System.out.println ("fin" );
lit.close(); ecr.close(); pout.close(); uc.disconnect();
} catch (MalformedURLException e) {
System.out.println("1 "+e);// bad postURL
} catch (IOException e2) {
System.out.println("2 "+e2);* * // I/O error
}
}
} |
Partager