C# 连接到维基百科 API 的 WebRequest
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/773029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebRequest to connect to the Wikipedia API
提问by NickJ
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.
这可能是一个非常简单的问题,但我似乎无法格式化 post 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();
}
采纳答案by Keltex
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:
您可能想先尝试 GET 请求,因为它更简单一些(您只需要 POST 即可登录维基百科)。例如,尝试模拟这个请求:
http://en.wikipedia.org/w/api.php?action=query&prop=images&titles=Main%20Page
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:
编辑:他在 POST 请求中遇到的另一个问题是,The exception is : The remote server returned an error: (417) Expectation failed.
可以通过设置解决:
System.Net.ServicePointManager.Expect100Continue = false;
(This is from: HTTP POST Returns Error: 417 "Expectation Failed.")
(这是来自:HTTP POST 返回错误:417“期望失败。”)
回答by Tommi Forsstr?m
You seem to be pushing the input data on HTTP POST, but it seems you should use HTTP GET.
您似乎在 HTTP POST 上推送输入数据,但您似乎应该使用 HTTP GET。
From the MediaWiki API docs:
来自 MediaWiki API 文档:
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
API 通过查询字符串中的参数获取其输入。每个模块(以及每个 action=query 子模块)都有自己的一组参数,这些参数在文档和 action=help 中列出,并且可以通过 action=paraminfo 检索。 http://www.mediawiki.org/wiki/API:Data_formats
回答by Dinis Cruz
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.
我目前正处于实现 C# MediaWiki API 的最后阶段,该 API 允许对大多数 MediaWiki 查看和编辑操作进行简单的脚本编写。
The main API is here: http://o2platform.googlecode.com/svn/trunk/O2%20-%20All%20Active%20Projects/O2_XRules_Database/_Rules/APIs/OwaspAPI.csand here is an example of the API in use:
主要 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;