HttpURLConnexion : server redirected to many times erreur
Fermé
Kilkenny95
Messages postés157Date d'inscriptionlundi 24 septembre 2018StatutMembreDernière intervention31 août 2020
-
Modifié le 4 mars 2020 à 14:03
Bonjour,
J'aimerai joindre et consommer une API (GET_URL) qui necessite une authentification. Quand je run mon code, j'obtiens l'erreur : server redirected to many times (20). Je suis dessus depuis des semaines sans trouver de solutions. Pourtant quand je test avec une API de test toute simple sans authent, ça marche. L'erreur se situe au niveau de mon
int responseCode = con.getResponseCode();
Le programme entier :
public class HttpConnexion2 {
private static final String GET_URL = "https://monAPI.com/";
public static void main(String[] args) throws IOException {
sendGET();
System.out.println("GET DONE");
}
private static void sendGET() throws IOException {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("monProxy", Port));
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("monAuthent",
"monCode".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
URL obj = new URL(GET_URL);
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection con = (HttpURLConnection) obj.openConnection(proxy);
con.setRequestMethod("GET");
con.setRequestProperty("Authorization", "maCléAPIencodée");
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
System.out.println(response.toString());
} else {
System.out.println("GET request not worked");
}
}
}
Merci d'avance
A voir également:
HttpURLConnexion : server redirected to many times erreur