Java - HttpURLConnection到立交桥服务器

问题描述:

我想建立一个与Java立交桥的服务器连接。但是,当我的请求参数中存在空白时,我总是收到错误的请求响应,如Bulach West。 我所使用的代码如下:Java - HttpURLConnection到立交桥服务器

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); 

请求,而空格做工精细。我现在能做什么? 最好的问候, 纳扎尔

我认为你必须字符串参数编码喜欢这里How do I encode url parameters

感谢您的帮助M8。我找到了一个工作解决方案

起初我试图使用java类URLEncoder编码我的网址,但由于“在 [\‘像某些特定的字符\铁路\’= \‘tram_stop \’]的编码的结果是。不正确

所以我所做的就是复制示例URL像 (http://www.overpass-api.de/api/interpreter?data=[out:xml];node[ \ “铁\”= \ “tram_stop \”] [\ “名称\”= \ “比拉赫西\”];出去; )

并将其传递到我的网络浏览器。在发送请求并获得正确响应后,我从我的Web浏览器复制链接把它转换成我的java代码。发送请求自动编码我的网址。

现在看起来像:(http://www.overpass-api.de/api/interpreter?data[out:xml];node[%22highway%22=%22bus_stop%22[%22name%22%20=%20%22Bulach+West%22];out;)