import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class SparqlGET {
  public static void main(String[] args) { try {
    BufferedReader br = null;
    URL inputURL = new URL("http://dbpedia.org/sparql?query=" +
          "select+distinct+?X+where+{?X+a+<http://dbpedia.org/ontology/Country>}");
    HttpURLConnection con = (HttpURLConnection) inputURL.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("Accept", "application/sparql-results+xml");
    con.connect();
    String s = "";   StringBuffer res= new StringBuffer();
    br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
    while ((s = br.readLine()) != null) { res.append(s+ "\n");	}
    br.close();
    System.out.println(res);  // or fill XML from Reader
  } catch (Exception e) { e.printStackTrace(); } }}
