【发布时间】:2013-02-21 11:14:17
【问题描述】:
在 Windows cURL 中,我可以发布类似这样的网络请求:
curl --dump-header cook.txt ^
--data "RURL=http=//www.example.com/r&user=bob&password=hello" ^
--user-agent "Mozilla/5.0" ^
http://www.example.com/login
使用type cook.txt 我会收到类似这样的回复:
HTTP/1.1 302 Found
Date: Thu, ******
Server: Microsoft-IIS/6.0
SERVER: ******
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Location: ******
Set-Cookie: Cookie1=; domain=******; expires=****** ******
******
******
Cache-Control: private
Content-Type: text/html; charset=iso-8859-1
Content-Length: 189
我可以手动读取 cookie 行,例如:Set-Cookie: AuthCode=ABC...(我当然可以编写脚本)。所以我可以使用AuthCode 进行后续请求。
我正在尝试在 R 中使用 RCurl 和/或 httr 做同样的事情(仍然不知道哪个更适合我的任务)。
当我尝试时:
library(httr)
POST("http://www.example.com/login",
body= list(RURL="http=//www.example.com/r",
user="bob", password="hello"),
user_agent("Mozilla/5.0"))
我收到类似这样的回复:
Response [http://www.example.com/error]
Status: 411
Content-type: text/html
<h1>Length Required</h1>
总的来说,我知道 411 错误,我可以尝试修复该请求;但我没有在 cURL 中得到它,所以我在 POST 命令中做错了。
你能帮我把我的 cURL 命令翻译成 RCurl 和/或 httr 吗?
【问题讨论】:
-
请提供一个可重现的示例 - 您没有提供有关如何处理身份验证的足够详细信息。 httr(如果我没记错的话)应该在调用同一站点时自动保留 cookie。