gpt4 book ai didi

java - 连接到代理后面的 DBpedia 端点会出现 407 错误?

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:40 26 4
gpt4 key购买 nike

我正在尝试连接到 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

最佳答案

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

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

Create a QueryExecution that will access a SPARQL service over HTTP

关于java - 连接到代理后面的 DBpedia 端点会出现 407 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com