Bonjour à tou(te)s,
Voilà mon p'tit problème: je suis censé écrire un programme Java pour récupérer en automatique un fichier sur un site sécurisé https. J'ai donc téléchargé la librairie HTTPClient de chez apache puis développé mon code.
Le problème que je rencontre n'est pas de savoir ce qu'il se passe UNE FOIS que je suis connecté sur ce site mais d'essayer d'arriver à me connecter sur ce site, en tout premier lieu; en effet, de chez moi en connexion directe, j'arrive à l'établir mais ce n'est pas le cas du boulot où j'utilise un proxy, donc je pense que mon problème vient de là. J'ai parcouru quelques forums et essayé d'implémenter certaines lignes de code qui sont censés régler ce problème mais ça ne marche toujous pas (No Route to host: connect, quoi que je fasse). Quelqu'un peut-il m'aider à integrer les quelques lignes qui me manquent?
Voici mon code:
public class Downloader {
public static void main(String[] args) {
int exitcode = 0;
int statusCode;
String url = "https://mon_fameux_site" + action;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
try {
Properties prop = new Properties();
String str = "C:/Documents and Settings/params.ini";
InputStream fis = (InputStream)new FileInputStrea(str);
prop.load(fis);
System.out.println("Fichier params.ini charge!");
// Set Credentials
UsernamePasswordCredentials credentials;
credentials = new UsernamePasswordCredential(prop.getProperty("username"),
prop.getProperty("password"));
System.out.println("Username: " + prop.getProperty("username")+ "\nPassword: "+ prop.getProperty("password"));
client.getState().setCredentials(new AuthScope("mon_fameux_site", 443), credentials);
// Executing the method.
statusCode = client.executeMethod(method);
System.out.println("Connexion assuree!");
if (statusCode != HttpStatus.SC_OK) {
// Handling HTTP error 404 and 500 not covered in this example
// All http error cause in this example exit with status 1.
System.err.println("Method failed: " + method.getStatusLine()+ "\n" +
method.getResponseBodyAsString());
System.out.println(method.getRequestCharSet() + "\n" + method.getRequestHeader("").toString());
exitcode = 1;
}
else {
//Get the file size from the response body and do something with it
Header[] contlen = method.getResponseHeaders("Content-Length");
if (contlen.length != 0) {
StringTokenizer stone = new StringTokenizer(contlen[0].getValue(), "=");
int size = new Integer(stone.nextToken()).intValue();
// Do something with the file size
System.out.println("File size is: " + size);
}
// Get the filename from the response body.
InputStream is = method.getResponseBodyAsStream();
Header[] contdisp = method.getResponseHeaders("Content-Disposition");
String filename = null;
StringTokenizer sttwo = new StringTokenizer(contdisp[0].getValue(), "=");
while (sttwo.hasMoreTokens()) filename = sttwo.nextToken();
// Hardcoded disk and directory path are indicative
FileOutputStream fos = new FileOutputStream("C:/Documents and Settings" + filename);
byte[] buffer = new byte[4096];
int count = is.read(buffer);
while (count != -1) {
fos.write(buffer, 0, count);
count = is.read(buffer);
}
fos.flush();
fos.close();
is.close();
}
} catch (HttpException e) {
exitcode = 2;
System.err.println("Fatal HTTP Error: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
exitcode = 3;
System.err.println("Fatal I/O error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
System.exit(exitcode);
}
System.out.println("Download done");
}
}
Partager