Closed. This question is not written in English. It is not currently accepting answers.
Stack Overflow is an English-only site. The author must be able to communicate in English to understand and engage with any comments and/or answers their question receives. Don’t translate this post for the author; machine translations can be inaccurate, and even human translations can alter the intended meaning of the post.
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.
< code > private static String calcularRotaOtimizada ( String origem, String destinos )
// 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" ) ;
BufferedReader responseReader = new BufferedReader ( new InputStreamReader ( connection. getInputStream ())) ;
String response = responseReader. lines () . collect ( Collectors. joining ()) ;
<code> 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;
}
</code>
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;
}