我正在尝试建立一个服务器连接以通过 Java 跨越。但是,当我的请求参数(如Bulach West)中有空格时,我总是会收到错误的请求响应。我使用的代码如下:
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);
没有空格的请求可以正常工作。我现在能做什么?最好的问候, 纳扎尔