10

これは哀れなほど単純な問題かもしれませんが、投稿の webrequest/response をフォーマットしてWikipedia APIからデータを取得することができないようです。誰かが私の問題を見るのを手伝ってくれるなら、私は自分のコードを以下に投稿しました。

    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();
    }
4

3 に答える 3

7

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

于 2009-04-21T15:11:07.397 に答える
1

私は現在、ほとんどのMediaWikiの表示および編集アクションの簡単なスクリプトを可能にするC#MediaWikiAPIの実装の最終段階にあります。

主なAPIは次のとおりです:http://o2platform.googlecode.com/svn/trunk/O2%20-%20All%20Active%20Projects/O2_XRules_Database/_Rules/APIs/OwaspAPI.cs使用中のAPIの例:

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;
于 2010-04-22T14:20:19.140 に答える
0

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

于 2009-04-21T15:10:20.833 に答える