import java.io.BufferedReader; import java.net.HttpURLConnection; import java.net.URL;
import java.io.InputStreamReader;  import java.io.OutputStreamWriter;
public class SparqlDirectPOST {
  public static void main(String[] args) { try {
    BufferedReader br = null;
    URL inputURL = new URL("http://www.lotico.com:3030/lotico/sparql");
    String q="select distinct ?C where {?X a ?C}";
    HttpURLConnection con = (HttpURLConnection) inputURL.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput(true);
    con.setRequestProperty("Content-Type", "application/sparql-query");
    con.setRequestProperty("Accept", "application/sparql-results+xml");
      OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
      wr.write(q); wr.flush(); wr.close();
    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(); } }}
