.net RDF DBLP查询

时间:2013-04-04 12:08:43

标签: rdf

public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        String st = "";
        String qry = "";
        String uri_V;
        String dguri_V;

        // uri_V = "http://dbpedia.org/sparql";
        // dguri_V = "http://dbpedia.org";

        uri_V = "http://dblp.l3s.de/d2r/sparql";

        // Modify if need......
        //String ns = "\""+TextBox1.Text.ToString()+"\"";
        // String qry = "SELECT DISTINCT ?name WHERE { ?person foaf:name ?name.FILTER regex(str(?name),"+ns+").}";

        qry = "SELECT ?x WHERE { ?x dcterms:issued ?year} LIMIT 20";
        //  qry = "SELECT ?name WHERE { ?x dcterms:issued ?year. FILTER (?year>2011). ?x dc:creator ?author. ?author foaf:name ?name. } LIMIT 20";
        /* if (radiosrch.SelectedIndex == 0)
         {
             qry = "SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 5";
         }
         else// if (radiosrch.SelectedIndex == 1) 
         {
             // qry= "query for publisher."
             qry = "SELECT DISTINCT ?Concept WHERE {[] a ?Concept} LIMIT 5";
         }
         */

        //Common
        SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(uri_V));//, dguri_V);
        SparqlResultSet results = endpoint.QueryWithResultSet(qry);
        foreach (SparqlResult result in results)
        {
            Console.WriteLine(result.ToString());
            st = st + result.ToString() + "\n";
        }
        TextBox3.Text = st.ToString();
    }
    catch (Exception ex)
    {
        Label1.Visible = true;
        Label1.Text = ex.ToString();
    }

}

}

我收到以下错误:
VDS.RDF.Query.RdfQueryException: A HTTP Error occurred while trying to make the SPARQL Query, see inner exception for details ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at System.Net.HttpWebRequest.GetResponse() at VDS.RDF.Query.SparqlRemoteEndpoint.ExecuteQuery(Uri target, String postData, String accept) at VDS.RDF.Query.SparqlRemoteEndpoint.QueryInternal(String sparqlQuery, String acceptHeader) at VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) --- End of inner exception stack trace --- at VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(ISparqlResultsHandler handler, String sparqlQuery) at VDS.RDF.Query.SparqlRemoteEndpoint.QueryWithResultSet(String sparqlQuery) at Index.ImageButton1_Click(Object sender, ImageClickEventArgs e) in d:\SPARQL\Index.aspx.cs:line 53

但是当我在http://dblp.l3s.de/d2r/snorql端点上运行此查询时,我绝对没有错误。 PLZ帮助我!!

1 个答案:

答案 0 :(得分:1)

我认为您的问题是您未能在SPARQL查询中定义任何PREFIX声明,但您在查询中使用了URI的前缀名称缩写。

因此,远程服务器完全有权返回400 Bad Request,因为它不知道你在问什么。

如果您不知道它已经尝试使用在线SPARQL Query Validator,它可以为您的查询提供有用的完整性检查。您输入的查询在输入时会出现以下错误:

  

第1行,第22列:未解析的前缀名称:dcterms:已发布

您的查询应符合以下条件:

String qry = @"PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?x WHERE { ?x dcterms:issued ?year} LIMIT 20";

您可能还想查看Querying with SPARQL文档,其中在Representing Queries部分中包含使用SparqlParameterizedString类来构建具有预定义前缀的查询的示例,例如。

SparqlParameterizedString queryString = new SparqlParameterizedString();
queryString.Namespaces.AddNamespace("dcterms", new Uri("http://purl.org/dc/terms/"));
queryString.CommandText = "SELECT ?x WHERE { ?x dcterms:issued ?year} LIMIT 20";

然后,当您准备将其传递到远程端点时,您只需要在ToString()上调用queryString