Ciao a tutti, ho sviluppato un programma per scaricare automaticamente (e rinominare) dei file da un sito.
Prima il download non richiedeva la registrazione al sito, ora invece la richiede.
Pensavo di simulare l'invio di una sessione tramite java (utilizzerei quella scaricata sul mio computer dal browser senza andare a fare il login tramite il mio programma e scaricare la nuova sessione da li) ma non so come fare 
Vi scrivo il mio programma (pulito, senza neanche i tentativi di login), spero mi possiate aiutare o indicare qualche "guida"!
codice:
import java.io.*;
import java.net.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class DManga {
public static void main(String[] args) throws IOException, InterruptedException {
String file="elenco.txt";
String log="log.txt";
String path="G:/Ebook/";
if(args.length!=0)
path=args[0];
String down="Manga/";
file=path+file;
log=path+log;
int nol= numOfLoop(file);
String[] arr=new String[nol];
ScriviSuFile("LOG: "+FindDate() +" ALLE " + FindOra(),log,true);
String userAgent = "Opera/9.63 (Windows NT 5.1; U; en) Presto/2.1.1";
BufferedReader fBR = new BufferedReader(new FileReader(file));
for(int i=0; i<nol;i++){
String riga,nomeFile,url;
int cap;
riga=fBR.readLine();
System.out.println(riga);
String[] splitta=riga.split(",");
cap=Integer.parseInt(splitta[2]);
cap++;
nomeFile=splitta[3]+" "+cap+" Cap.cbz";
url=splitta[0]+splitta[1]+"/"+cap+"/";
//DOWNLOAD
try {
System.out.println("Downloading \""+nomeFile+"\" ...");
downloadFromUrl(new URL(url), nomeFile, userAgent, path+down);
System.out.println("OK");
ScriviSuFile("- Scaricato "+nomeFile+" alle "+FindOra(),log,true);
//RIEDITO IL NOME DA SALVARE
riga=splitta[0]+","+splitta[1]+","+cap+","+splitta[3];
} catch (Exception e) {
System.out.println("Nessun nuovo capitolo per \""+splitta[3]+"\"..");
ScriviSuFile("Nessun nuovo capitolo per \""+splitta[3]+"\".."+ "(ore "+FindOra()+")",log,true);
}
arr[i]=riga;
}
updateFile(arr,file);
ScriviSuFile("",log,true);
}
private static String FindDate() {
Calendar cal = new GregorianCalendar();
int giorno = cal.get(Calendar.DAY_OF_MONTH);
int mese = cal.get(Calendar.MONTH);
int anno = cal.get(Calendar.YEAR);
return giorno + "-" + (mese + 1) + "-" + anno ;
}
private static String FindOra() {
Calendar calendar = new GregorianCalendar();
String orario;
int ore = calendar.get(Calendar.HOUR);
int minuti = calendar.get(Calendar.MINUTE);
int secondi = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
orario = "A.M.";
else
orario = "P.M.";
return ore + ":" + minuti + ":" + secondi + " " + orario ;
}
private static void updateFile(String[] arr,String file) throws IOException {
boolean a=false;
for(int i=0;i<arr.length;i++){
ScriviSuFile(arr[i],file,a);
a=true;
}
}
public static boolean ScriviSuFile(String a,String file,boolean append){
/*
* Definisco la scrittura su file
* Se il booleano che mi arriva come parametro è true allora scrivo a capo nel file
* altrimenti cancello il file e riscrivo da capo
*/
try {
PrintWriter out = new PrintWriter(new FileWriter(file, append));
out.println(a);
out.close();
return true;
}
catch(Exception e){return false;}
}
private static int numOfLoop(String file) throws IOException {
int i=0;
BufferedReader fBR = new BufferedReader(new FileReader(file));
do{
i++;
}while(fBR.readLine()!=null);
return i-1;
}
public static void downloadFromUrl(URL url, String localFilename, String userAgent, String path)
throws IOException {
InputStream is = null;
FileOutputStream fos = null;
try {
URLConnection urlConn = url.openConnection();
if (userAgent != null) {
urlConn.setRequestProperty("User-Agent", userAgent);
}
is = urlConn.getInputStream();
fos = new FileOutputStream(path+localFilename);
byte[] buffer = new byte[4096];
int len;
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} finally {
try {
if (is != null) {
is.close();
}
} finally {
if (fos != null) {
fos.close();
}
}
}
}
}
Nel File elenco.txt son presente stringhe di questo tipo:
[edited]