开发者

Mediawiki action=parse with POST request

开发者 https://www.devze.com 2023-04-01 08:03 出处:网络
Did anyone try to make access to \"api.php?action=parse&text=\" page with the POST request? on the wiki documentation I have found that it\'s possible to convert wiki text to HTML. It works prett

Did anyone try to make access to "api.php?action=parse&text=" page with the POST request?

on the wiki documentation I have found that it's possible to convert wiki text to HTML. It works pretty good with GET request, but as I can understand has restriction on the text's length, so I've tried to do this through the POST. But unfortunately without any success... I get error message:

The remote server returned an error: (417) Expectation failed.

here is the code in C# that I use for the request:

StringBuilder postData = new StringBuilder();
foreach(va开发者_开发百科r param in parameters)
{
    if (postData.Length > 0)
        postData.Append("&");

    postData.AppendFormat("{0}={1}", param.Key, EncodeUrl(param.Value));
}
byte[] data = Encoding.UTF8.GetBytes(postData.ToString());

HttpWebRequest rq = (HttpWebRequest)WebRequest.Create(wiki.WikiURI + "/" + pgname);
rq.UserAgent = "Test Wiki Access" + Utils.Version.ToString();
rq.ContentType = "application/x-www-form-urlencoded";
rq.Method = "POST";
rq.ContentLength = data.Length;
Stream stream = rq.GetRequestStream();

stream.Write(data, 0, data.Length);
stream.Flush();
stream.Close();

string res = new StreamReader(rq.GetResponse().GetResponseStream(), Encoding.UTF8)
    .ReadToEnd();

Just wondering is it the problem in my code or that page doesn't support POST requests at all? Did anyone try to do the same?

Thanks in advance for any suggestion, Alex


Per the API FAQ:

Why does my API call on Wikimedia wikis just return an HTML error?

If you use API calls with POST requests make sure that these requests don't use Content-Type: multipart/form-data. This happens for instance if you use CURL to access the API and you pass your POST parameters as an array. The Squid proxy servers which are used at frontend servers at the Wikimedia wiki farm don't handle that correctly, thus an error is returned.

Instead, use the "value1=key1&value2=key2..." notation to pass the parameters as a string, similar to GET requests.

On other wikis which you access directly it doesn't make a difference.

What that means is that you need to add the following:

rq.ContentType = "application/x-www-form-urlencoded";

EDIT: The above is required for the query to work properly, but it's not what's causing the error. That's the fact that .Net adds a Expect: 100-continue header by default, which doesn't work with Wikipedia for some reason. To fix that add the following line before you make a request for the first itme:

ServicePointManager.Expect100Continue = false;
0

精彩评论

暂无评论...
验证码 换一张
取 消