I am trying to build up a server connection to overpass with Java. But I always get a bad request response when there is a white space in my request parameter like Bulach West. The code I used is following:
StringBuilder content = new StringBuilder();
URL url = new URL("http://www.overpass-api.de/api/interpreter?data=[out:xml];node[\"railway\"=\"tram_stop\"][\"name\" = \"Bulach West\"];out;");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
System.out.println("Content-Type: " +urlConnection.getContentType());
System.out.println("Content-Length: " + urlConnection.getContentLength());
System.out.println( "Date: " +new Date(urlConnection.getDate()));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
urlConnection.connect();
while ((line = bufferedReader.readLine()) != null) {
content.append(line + "\n");
}
bufferedReader.close();
System.out.println("output:\n "+content);
Requests without whitespaces work fine. What can I do now? Best regards, Nazar
I think you have to encode the string parameter like here How do I encode url parameters