0

我如何将 Special:Export 用于我的目的。我从我的应用程序发送下一个标题:

string request = "POST en.wikipedia.org/w/index.php?title=Special:Export&action=submit HTTP/1.1\r\n"
                 "Host: en.wikipedia.org\r\n"
                 "Content-Length: 32\r\n"
                 "Content-Type: application/x-www-form-urlencoded\r\n"
                 "Connection: close\r\n\r\n"
                 "catname=&pages=ukraine&curonly=1";

但它向我呈现有关错误的页面:

HTTP/1.0 400 错误请求服务器:squid/2.7.STABLE9 日期:2012 年 4 月 23 日星期一 14:45:12 GMT 内容类型:text/html 内容长度:3111 X-Squid-Error:ERR_INVALID_URL 0 X-Cache:来自 amssq46.esams.wikimedia.org 的 MISS X-Cache-Lookup:来自 amssq46.esams.wikimedia.org 的无:80 连接:关闭

我需要创建工具来替换 inwiki 链接以帮助翻译。我确定已经存在一些这样的工具,但我想自己做。

编辑:我使用 c++ 和套接字。

编辑:新要求:

string request = "POST https://en.wikipedia.org/w/index.php?title=Special:Export&action=submit HTTP/1.1\r\n"
//string request = "GET https://en.wikipedia.org/w/index.php?title=Special:Export&pages=ukraine&curonly=1\r\n"
                 "Host: en.wikipedia.org\r\n"
                 "User-Agent: MyCoolTool\r\n"
                 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
                 "Accept-Language: en-us,en;q=0.5\r\n"
                 "Accept-Encoding: gzip, deflate\r\n"
                 "Content-Length: 32\r\n"
                 "Content-Type: application/x-www-form-urlencoded\r\n"
                 "Connection: close\r\n\r\n"
                 "catname=&pages=Ukraine&curonly=1";

当我尝试获取页面时

string request = "GET http://en.wikipedia.org/wiki/Ukraine\r\n" ...
                 "User-Agent: YolaTool/0.1\r\n" ...

我有

脚本应使用包含联系信息的用户代理字符串,否则它们可能会被 IP 阻止,恕不另行通知。

4

1 回答 1

1

我建议您使用直接支持 HTTP 的高级网络库是有原因的:这样您就不必处理低级细节,并且可以确保它们是正确的。

在您的情况下,问题是在POST关键字之后,必须有一个绝对 URI:

POST http://en.wikipedia.org/w/index.php?title=Special:Export&action=submit HTTP/1.1

或者,更常见的是绝对路径:

POST /w/index.php?title=Special:Export&action=submit HTTP/1.1

请参阅RFC 2616,第 5.1.2 节

如果您修复此问题,您将收到 403 错误,但这次错误消息清楚地说明了如何解决此问题:

脚本应使用包含联系信息的用户代理字符串,否则它们可能会被 IP 阻止,恕不另行通知。

请参阅维基媒体用户代理政策

于 2012-04-23T19:37:21.543 回答