Java - HttpRequest to overpass server

Hello everyone,

I have got a little problem. 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("[out:xml];node[\"railway\"=\"tram_stop\"][\"name\" = \"Bulach West\"];out;");
	HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
	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;

	while ((line = bufferedReader.readLine()) != null) {
		   content.append(line + "\n");
	 System.out.println("output:\n "+content); 

Requests without whitespaces work fine. What can I do now?

Best regards

Shouldn’t you “URL encode” the string that is passed to new URL() ? Shouldn’t each space be replaced with %20 ?

Hello escada,

I found a working solution.
At first I tried to use the Java class


to encode my url, but because of some specific characters like



 [\"railway\"= \"tram_stop\"]

the encoded result was not correct.

So what I did is to copy an example url like[out:xml];node["railway"="tram_stop"]["name" = "Bulach West"];out;
and pass this into my web browser. After sending the request and getting a correct response, I copied the link from my web browser and added
it into my java code. Sending the request encoded my url automatically. Now it looks like:[out:xml];node[%22highway%22=%22bus_stop%22[%22name%22%20=%20%22Bulach+West%22];out;

Best regards,