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