WebRequest to connect to the Wikipedia API

前端 未结 3 1683
北海茫月
北海茫月 2021-02-14 13:54

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

相关标签:
3条回答
  • 2021-02-14 14:11

    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;
    
    0 讨论(0)
  • 2021-02-14 14:17

    You seem to be pushing the input data on HTTP POST, but it seems you should use HTTP GET.

    From the MediaWiki API docs:

    The API takes its input through parameters in the query string. Every module (and every action=query submodule) has its own set of parameters, which is listed in the documentation and in action=help, and can be retrieved through action=paraminfo. http://www.mediawiki.org/wiki/API:Data_formats

    0 讨论(0)
  • 2021-02-14 14:31

    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.")

    0 讨论(0)
提交回复
热议问题