【问题标题】:Java - HttpURLConnection to overpass serverJava - 跨越服务器的 HttpURLConnection
【发布时间】:2015-06-17 13:29:10
【问题描述】:

我正在尝试建立一个服务器连接以使用 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); 

没有空格的请求可以正常工作。我现在能做什么? 最好的祝福, 纳扎尔

【问题讨论】:

    标签: java httprequest bad-request http-response-codes overpass-api


    【解决方案1】:

    我认为你必须像这里 How do I encode url parameters 那样对字符串参数进行编码

    【讨论】:

      【解决方案2】:

      感谢m8的帮助。我找到了一个可行的解决方案。

      起初我尝试使用 java 类 URLEncoder 来编码我的网址,但由于 [\"railway\"= \"tram_stop 中的一些特定字符,如 \" \"] 编码结果不正确。

      所以我所做的就是复制一个示例网址,例如 (http://www.overpass-api.de/api/interpreter?data=[out:xml];node[\"railway\"=\"tram_stop\"][\"name\" = \"Bulach West\"];out;)

      并将其传递到我的网络浏览器中。发送请求并获得正确响应后,我从网络浏览器中复制了链接并将其添加到我的 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;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 2017-04-19
        • 1970-01-01
        • 1970-01-01
        • 2011-06-30
        • 2012-08-28
        • 1970-01-01
        相关资源
        最近更新 更多