【问题标题】:Connecting to DBpedia endpoint behind a Proxy gives 407 error?连接到代理后面的 DBpedia 端点会出现 407 错误?
【发布时间】:2015-04-15 11:37:14
【问题描述】:

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

【问题讨论】:

    标签: java sparql dbpedia apache-jena


    【解决方案1】:

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

    QueryExecution sparqlService(字符串服务,Query查询,HttpAuthenticator认证器)

    创建一个 QueryExecution,它将 通过 HTTP 访问 SPARQL 服务

    【讨论】:

    • 我不知道怎么用,你能告诉我正确的方法吗?
    • @user1583465 我自己从未使用过它。我只是猜测,由于您的错误是“需要代理身份验证”,因此名称为“HttpAuthenticator”的对象可能是一个解决方案。不过,这只是一个猜测。
    【解决方案2】:

    (谁来这里寻找解决方案:连接到代理后面的 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();
        }
    

    【讨论】:

      猜你喜欢
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多