我正在尝试连接到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发布于 2015-04-15 19:59:34
有一个接受HttpAuthenticator的版本。也许您可以使用它来处理所需的任何身份验证?
QueryExecution sparqlService(https://jena.apache.org/documentation/javadoc/arq/com/hp/hpl/jena/query/QueryExecutionFactory.html#sparqlService(java.lang.String,%20com.hp.hpl.jena.query.Query,%20org.apache.jena.atlas.web.auth.HttpAuthenticator%29)(String服务、查询查询、HttpAuthenticator验证器)创建将通过HTTP
访问SPARQL服务的QueryExecution
发布于 2020-01-17 00:22:17
(对于来这里寻找解决方案的人:在代理后面连接到DBpedia端点)
您的代码本身就有解决方案。在调用.execSelect()之前添加以下行:
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
}
});测试和工作方法:
public static void main(String[] args) {
final String proxyUsername = "myUser";
final String proxyPassword = "myPwd";
final String proxyHost = "hostName";
final String proxyPort = "port";
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:tank;\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.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyUsername, proxyPassword.toCharArray());
}
});
ResultSet results = qexec.execSelect();
}https://stackoverflow.com/questions/29649189
复制相似问题