I want to integrate an API to define routes in my software using Java, and whenever I try to run the program the error appears: java.io.IOException: Server returned HTTP response code: 403 for URL
Well, I’m trying to make a program that uses the origin and destination coordinates to create a route that passes through all points and shows the distance between the points. But when I’m running the program, an Exception error occurs when making the API call, and I’ve already checked the key and the URL are correct.
private static String calcularRotaOtimizada(String origem, String destinos)
throws Exception {
// Construção da URL da API OpenRouteService para calcular rota
String url = "https://api.openrouteservice.org/v2/directions/driving-car?
api_key=*******************=" + URLEncoder.encode(origem, "UTF-8") + "&end=" + URLEncoder.encode(destinos, "UTF-8");
// Criação de uma conexão HTTP
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
// Leitura da resposta
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response = responseReader.lines().collect(Collectors.joining());
responseReader.close();
return response;
}
Romeu Sassusso is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.