使用 apache jena 发送 post 请求

Sending post request using apache jena

我正在使用以下代码向 sparql 端点发送请求。这些请求是 GET 请求。当我发送查询时,出现以下错误:414 Request-URI Too Large。我发现我可以通过发送 post 请求来解决问题。需要知道如何发送 post 请求。

Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryExecution qexec = QueryExecutionFactory.sparqlService(this.endPoint, query);
ResultSet resultSet = qexec.execSelect();

我正在使用以下查询:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>  
PREFIX yago: <http://dbpedia.org/class/yago/> 
SELECT ?entityName ?categoryName
WHERE { VALUES ?uri {<http://dbpedia.org/resource/Category:Roger_Federer> } 
      {?uri  (dcterms:subject|rdf:type) ?category .  
      OPTIONAL {?category rdfs:label ?categoryName . 
                ?uri rdfs:label ?entityName. } 
      } 
      FILTER(lang(?entityName) = 'en' && lang(?categoryName) = 'en') 
      FILTER (?category = dbpedia-owl:TennisPlayer || 
              ?category = yago:TennisOrganisations || 
              ?category =<http://dbpedia.org/resource/Category:The_Championships,_Wimbledon>
             )
     } 

在 uri 的值中,这些值由系统动态填充,它们的数量很大,这增加了查询的长度。

设置HttpQuery.urlLimit,虽然默认是2K字节,通常没问题。 (当前版本)。

我找到了以下问题的解决方案。我使用 QueryEngineHttp 发送 http post 查询并且有效。

Query query = QueryFactory.create(queryString, Syntax.syntaxARQ);
QueryEngineHTTP qeh = new QueryEngineHTTP(this.endPoint, query);
ResultSet resultSet = qeh.execSelect();