2014-01-15 21 views
1

我正在嘗試使用Jena ARQ和PreemptiveBasicAuthenticator,但沒有成功,任何人都可以提供幫助嗎?使用Jena ARQ使用基本身份驗證訪問遠程回購 - 搶先

我總是得到一個401,雖然通過休息客戶端,或使用openrdf的作品相同的請求。這可能與apache http客戶端有關並需要設置AuthScope?

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.ResultSet; 
import org.apache.jena.atlas.web.auth.HttpAuthenticator; 
import org.apache.jena.atlas.web.auth.PreemptiveBasicAuthenticator; 
import org.apache.jena.atlas.web.auth.ScopedAuthenticator; 

import java.net.URI; 

public class JenaConnect { 

    private final static String SPARQLR_ENDPOINT = "https://repo-eh-01.sparqlr.com/repositories/examples-repo"; 
    private final static String SPARQLR_USERNAME = "examples-repo"; 
    private final static String SPARQLR_PASSWORD = "XXXX"; 

    public static void main(String[] args) throws Exception { 

     String queryString = "SELECT * WHERE {?s ?p ?o}"; 
     Query query = QueryFactory.create(queryString); 
     HttpAuthenticator authenticator = new PreemptiveBasicAuthenticator(
       new ScopedAuthenticator(new URI(SPARQLR_ENDPOINT), SPARQLR_USERNAME, SPARQLR_PASSWORD.toCharArray()) 
     ); 
     QueryExecution queryExecution = QueryExecutionFactory.sparqlService(SPARQLR_ENDPOINT, query, authenticator); 
     try { 
      ResultSet results = queryExecution.execSelect(); 
      int i = 0; 
      while(results.hasNext()) { 
       results.next(); 
       i++; 
      } 
      System.out.println(i); 
     } finally { 
      queryExecution.close(); 
     } 
    } 
} 
+0

您可以打開'DEBUG'級記錄您的代碼(或專門爲'org.apache.http'),因爲這會再打印HTTP痕跡,這些日誌將允許您識別是否實際使用了身份驗證 – RobV

回答

相關問題