2

I am using the WebClient.DownloadString() method to download some data. I am using the following code:

static void Main(string[] args)
    {
        string query = "select+%3farticle+%3fmesh+where+{+%3farticle+a+npg%3aArticle+.+%3farticle+npg%3ahasRecord+[+dc%3asubject+%3fmesh+]+.+filter+regex%28%3fmesh%2c+\"blood\"%2c+\"i\"%29+}";
        NameValueCollection queries = new NameValueCollection();
        queries.Add("query", query);
        //queries.Add("output", "sparql_json");
        using (WebClient wc = new WebClient())
        {
            wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            wc.QueryString = queries;
            string result = wc.DownloadString("http://data.nature.com/sparql");
            Console.WriteLine(result);
        }
        Console.ReadLine();
    }

With this code, tt works fine and gives me an xml string as the output. But I would like to get a JSON output and hence I un-commented the line

queries.Add("output", "sparql_json");

and executed the same program and it seems to be fetching an error message from the server.

However, if I try to use a web browser and use the same url (as given below), it gives me a JSON as expected: URL that works in browsers

I am wondering what the problem could be. Especially when it works in a browser and not using a webclient. Is the webclient doing something different here?

Please note that I also tried to specify the query as

query + "&output=sparql_json"

But that does not work either.

Could someone please tell me what the problem might be?

Thanks

6
  • in your string query = "..." initialization, should you be encoding the value already? I don't think you should be. Put it as plain-text and let queries.Add("query", query) encode it for you. Commented Apr 5, 2012 at 17:08
  • Matthew, the problem is not with the query. With just the query, it works fine - it gives me an xml. But if i try to do an "&output=sparql_json" along with the query, i get an error. Any ideas? Commented Apr 5, 2012 at 17:11
  • It could be the browser fixing up a faulty query string for you automatically, pretty sure it's your query string that needs fixing. Commented Apr 5, 2012 at 17:17
  • Matthew, Thanks a lot for the comments. Apparently, as L.B suggested, I just had to include the Accept Json header and changing nothing else. The query string (encoded or not) gives me just the same result. Commented Apr 5, 2012 at 17:23
  • Glad you got it working. Commented Apr 5, 2012 at 17:24

1 Answer 1

6

Add wc.Headers.Add("Accept","application/json");. Here is the full source I tested

string query = "select ?article ?mesh where { ?article a npg:Article . ?article npg:hasRecord [ dc:subject ?mesh ] . filter regex(?mesh, \"blood\", \"i\") }";
NameValueCollection queries = new NameValueCollection();
queries.Add("query", query);
queries.Add("output", "sparql_json");
using (WebClient wc = new WebClient())
{
    wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    wc.Headers.Add("Accept","application/json");
    wc.QueryString = queries;
    string result = wc.DownloadString("http://data.nature.com/sparql");
    Console.WriteLine(result);
}
Console.ReadLine();
Sign up to request clarification or add additional context in comments.

2 Comments

Thats amazing. I have downloaded a json before but I never specified the Headers.Add("Accept","application/json"); Does it have anything to do with the server? Anyways, the solution worked. Thanks a lot!!
To add to my previous comment, the solution was to add the extra header that you suggested : wc.Headers.Add("Accept","application/json"); made all the difference. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.