Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebRequest to connect to the Wikipedia API

This may be a pathetically simple problem, but I cannot seem to format the post webrequest/response to get data from the Wikipedia API. I have posted my code below if anyone can help me see my problem.

    string pgTitle = txtPageTitle.Text;

    Uri address = new Uri("http://en.wikipedia.org/w/api.php");

    HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    string action = "query";
    string query = pgTitle;

    StringBuilder data = new StringBuilder();
    data.Append("action=" + HttpUtility.UrlEncode(action));
    data.Append("&query=" + HttpUtility.UrlEncode(query));

    byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

    request.ContentLength = byteData.Length;

    using (Stream postStream = request.GetRequestStream())
    {
        postStream.Write(byteData, 0, byteData.Length);
    }

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        // Get the response stream.
        StreamReader reader = new StreamReader(response.GetResponseStream());

        divWikiData.InnerText = reader.ReadToEnd();
    }
like image 492
NickJ Avatar asked Apr 21 '09 15:04

NickJ


People also ask

How do I access my API on Wikipedia?

For example, the English Wikipedia is at https://en.wikipedia.org/w/api.php . For testing new accounts or test edits to pages, use the test wiki endpoint https://test.wikipedia.org/w/api.php .

Is Wikipedia API free?

Wikipedia and other Wikimedia projects are free, collaborative repositories of knowledge, written and maintained by volunteers from around the world. The Wikimedia API gives you open access to add this free knowledge to your projects and apps.

Does Wikipedia has an API?

What is the Wikipedia API? The Wikipedia API (official documentation) is supported by the MediaWiki's API and provide access to Wikipedia and other MediaWiki data without interacting with the user interface.

How does Wikipedia get content?

Written collaboratively by largely anonymous volunteers, anyone with Internet access and in good standing can write and make changes to Wikipedia articles (except in limited cases where editing is restricted to prevent disruption or vandalism).


2 Answers

You might want to try a GET request first because it's a little simpler (you will only need to POST for wikipedia login). For example, try to simulate this request:

http://en.wikipedia.org/w/api.php?action=query&prop=images&titles=Main%20Page

Here's the code:

HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create("http://en.wikipedia.org/w/api.php?action=query&prop=images&titles=Main%20Page");
using (HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse())
{
    string ResponseText;
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        ResponseText = reader.ReadToEnd();
    }
}

Edit: The other problem he was experiencing on the POST request was, The exception is : The remote server returned an error: (417) Expectation failed. It can be solved by setting:

System.Net.ServicePointManager.Expect100Continue = false;

(This is from: HTTP POST Returns Error: 417 "Expectation Failed.")

like image 154
Keltex Avatar answered Nov 08 '22 07:11

Keltex


I'm currently in the final stages of implementing an C# MediaWiki API which allows the easy scripting of most MediaWiki viewing and editing actions.

The main API is here: http://o2platform.googlecode.com/svn/trunk/O2%20-%20All%20Active%20Projects/O2_XRules_Database/_Rules/APIs/OwaspAPI.cs and here is an example of the API in use:

var wiki = new O2MediaWikiAPI("http://www.o2platform.com/api.php");

wiki.login(userName, password);

var page = "Test"; // "Main_Page";

wiki.editPage(page,"Test content2");

var rawWikiText = wiki.raw(page);
var htmlText = wiki.html(page);

return rawWikiText.line().line() + htmlText;
like image 20
Dinis Cruz Avatar answered Nov 08 '22 07:11

Dinis Cruz