连接到代理服务器后面的DBpedia端点会出现407错误?

时间:2015-04-15 11:37:15

标签: java sparql dbpedia apache-jena

我正在尝试连接到DBpedia以使用apache jena运行sparql查询。我在代理服务器后面,问题是当我使用apache jena连接我的代码时出错,但我可以使用直接url建立连接。 这段代码正在运行。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;

/**
 *
 * @author user
 */
public class NewClass {
     public static void main(String[] args) throws Exception {
         System.setProperty("http.proxyHost", "10.25.0.42");
    System.setProperty("http.proxyPort", "3128");
     Authenticator.setDefault(new Authenticator()
    {
    protected PasswordAuthentication getPasswordAuthentication()
    {
    return new PasswordAuthentication("asiddh-me-13","*****".toCharArray());
    }
    });
        URL oracle = new URL("http://dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fdbpedia.org&query=select++%3Fx+%3Fy+%3Fc+%3Fp%0D%0Awhere%7B%0D%0A%3Fx+dbpedia-owl%3AwikiPageDisambiguates+dbpedia%3ASOAP%3B%0D%0A+dbpedia-owl%3AwikiPageDisambiguates+%3Fy.%0D%0A%3Fy+dbpedia-owl%3Aabstract+%3Fc.%0D%0A%3Fy+dbpedia-owl%3Athumbnail+%3Fp.%0D%0Afilter%28langmatches%28lang%28%3Fc%29%2C%22en%22%29%29%0D%0A%7D&format=text%2Fhtml&timeout=30000&debug=on");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) 
            System.out.println(inputLine);
        in.close();
    }
}

但是当我尝试使用Jena api进行连接时,它会给我错误。

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import java.net.Authenticator;
import java.net.PasswordAuthentication;


public class Sparqldbpedia {
    public static void main(String[] args) {


System.setProperty("http.proxyHost", "10.25.0.42");
System.setProperty("http.proxyPort", "3128");
 Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("asiddh-me-13","****".toCharArray());
}
});


 String keyword="";
 keyword="tank";




 String sparqlQueryString = "PREFIX p: <http://dbpedia.org/property/>"+
"PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>"+
"PREFIX dbpedia: <http://dbpedia.org/resource/>"+
"PREFIX category: <http://dbpedia.org/resource/Category:>"+
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"+
                "select  ?x ?y ?c ?p\n" +
"where{\n" +
"?x dbpedia-owl:wikiPageDisambiguates dbpedia:"+keyword+ ";\n" +
" dbpedia-owl:wikiPageDisambiguates ?y.\n" +
"?y dbpedia-owl:abstract ?c.\n" +
"?y dbpedia-owl:thumbnail ?p.\n" +
"filter(langmatches(lang(?c),\"en\"))\n" +
"}";
        Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);

    System.out.println("try block");
    try {
        ResultSet results = qexec.execSelect();
        for ( ; results.hasNext() ; )
    {
        QuerySolution soln = results.nextSolution() ;
        String x = soln.get("?x").toString();
        String y = soln.get("?y").toString();
        String c = soln.get("?c").toString();
        String p = soln.get("?p").toString();

        System.out.print(x +"\t"+y+"\t"+c+"\t"+p+"\n");
    }

    }catch(Exception e){System.out.println("catch error"+e.getMessage());}
    finally { qexec.close() ; }




}

}

错误是:

HTTP 407 error making the query: Proxy Authentication Required

1 个答案:

答案 0 :(得分:0)

sparqlService的版本接受HttpAuthenticator。也许您可以使用它来处理所需的身份验证?

  

QueryExecution sparqlService(String service,Query query,HttpAuthenticator authenticator)

     

创建一个将执行的QueryExecution   通过HTTP访问SPARQL服务