7 * Java HTTP クライアントサンプル - HttpURLConnection 版 -
9 * @author 68user http://X68000.q-e-d.net/~68user/
11 public class HttpPOST {
12 //public static String host = "http://api06.dev.openstreetmap.org";
13 //public static String host = "http://api.openstreetmap.org";
14 public static String host = "http://overpass-api.de";
16 public static void main(String[] args) throws MalformedURLException, ProtocolException, IOException {
17 double minlat = 35.13d;
18 double maxlat = 35.66d;
19 double minlon = 138.99d;
20 double maxlon = 139.79d;
21 getCapabilities(minlat, maxlat, minlon, maxlon);
24 public static void getCapabilities(double minLat, double maxLat, double minLon, double maxLon) throws MalformedURLException, ProtocolException, IOException {
25 System.out.println(host + "/api/interpreter");
26 URL url = new URL(host + "/api/interpreter");
28 HttpURLConnection urlconn = (HttpURLConnection)url.openConnection();
29 urlconn.setRequestMethod("POST");
30 urlconn.setDoOutput(true); // POSTのデータを後ろに付ける
31 urlconn.setInstanceFollowRedirects(false); // 勝手にリダイレクトさせない
32 urlconn.setRequestProperty("Accept-Language", "ja;q=0.7,en;q=0.3");
33 urlconn.setRequestProperty("Content-Type","text/xml;charset=utf-8");
37 PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(urlconn.getOutputStream(), "utf-8")));
38 pw.print("<osm-script timeout=\"900\" element-limit=\"1073741824\">");
40 pw.print("<query type=\"node\">");
41 pw.print("<has-kv k=\"highway\" v=\"bus_stop\"/>");
42 pw.print("<bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
44 pw.print("<query type=\"node\">");
45 pw.print("<has-kv k=\"public_transport\" v=\"platform\"/>");
46 pw.print("<has-kv k=\"bus\" v=\"yes\"/>");
47 pw.print("<bbox-query s=\"" + minLat + "\" n=\"" + maxLat + "\" w=\"" + minLon + "\" e=\"" + maxLon + "\"/>");
51 pw.print("</osm-script>");
52 pw.close(); // closeで送信完了
54 System.out.println("レスポンスコード[" + urlconn.getResponseCode() + "] " +
55 "レスポンスメッセージ[" + urlconn.getResponseMessage() + "]");
56 System.out.println("\n---- ボディ ----");
58 BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
60 File oFile = new File("existing.xml");
61 BufferedWriter hw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(oFile), "UTF-8"));
63 String line = reader.readLine();