Silly me,

I just forgot to urlencode the query string. Once that was done the  
rest of the code works!



On Apr 13, 2008, at 1:58 PM, Andrew (Chuan) Khoo wrote:

> Hi all,
>
> I wonder if anyone here has experience going through this problem
> before.
>
> I'm trying to access my locally-installed sparql endpoint through the
> URL object in  java, and it always returns me with a http 400 error,
> even though tests with just a simple browser query (get or post) will
> work properly. Here is my test java code:
>
>
>       URL queryURL;
>       URLConnection urlConn;
>
>       public SparqlQuery() {
>
>               String msg = "default-graph-uri=http://
> dbpedia.org&format=application/sparql-results
> +xml&debug=on&query=SELECT ?p ?o WHERE { 
> <http://dbpedia.org/resource/Metropolitan_Museum_of_Art
>> ?p ?o }";
>               byte[] msgAsBytes = msg.getBytes();
>               
>               try {
>                       queryURL = new URL("http://localhost:8890/sparql";);
>                       urlConn = queryURL.openConnection();
>                       ((HttpURLConnection) urlConn).setRequestMethod("POST");
>                       
>                       urlConn.setDoOutput(true);
>                       urlConn.setDoInput(true);
>                       urlConn.setUseCaches(false);
>
>                       OutputStream oStream = urlConn.getOutputStream();
>                       oStream.write(msgAsBytes);
>                       oStream.flush();
>
>                       BufferedReader in = new BufferedReader(new
> InputStreamReader(urlConn.getInputStream()));
>
>                       String aLine = "";
>                       while ((aLine = in.readLine()) != null) {
>                               System.out.println(aLine);                      
>         
>                       }
>                       
>                       in.close();
>                       oStream.close();                        
>                                               
>               } catch (MalformedURLException e) {
>                       e.printStackTrace();
>               } catch (IOException e) {
>                       // TODO Auto-generated catch block
>                       e.printStackTrace();
>               }
>               
>       }
>
>
> Some help on working with this will be very much appreciated. As I've
> mentioned, accessing the dbpedia data via standard http requests, and
> even via actionscript works pretty well, but not java.
>
>
> Thanks,
> Andrew
>
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by the 2008 JavaOne(SM) Conference
> Don't miss this year's exciting event. There's still time to save  
> $100.
> Use priority code J8TL2D2.
> http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
> _______________________________________________
> Dbpedia-discussion mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/dbpedia-discussion


-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
Dbpedia-discussion mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dbpedia-discussion

Reply via email to