【问题标题】:Post request using cookies with cURL, RCurl and httr使用带有 cURL、RCurl 和 httr 的 cookie 发布请求
【发布时间】: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。

标签: r curl libcurl rcurl httr


【解决方案1】:

httr 会在对同一站点的调用中自动保留 cookie,如这两个对 http://httpbin.org 的调用所示

GET("http://httpbin.org/cookies/set?a=1")
# Response [http://httpbin.org/cookies]
#   Status: 200
#   Content-type: application/json
# {
#    "cookies": {
#     "a": "1"
#   }
# } 

GET("http://httpbin.org/cookies")
# Response [http://httpbin.org/cookies]
#   Status: 200
#   Content-type: application/json
# {
#   "cookies": {
#     "a": "1"
#   }
# } 

也许问题在于您以application/x-www-form-urlencoded 发送数据,但httr 中的默认值为multipart/form-data,因此在您的POST 调用中使用multipart = FALSE

【讨论】:

【解决方案2】:

根据 Juba 的建议,这是一个有效的 RCurl 模板。

代码模拟浏览器行为,因为它:

  1. 在登录屏幕上检索 cookie 并
  2. 在包含实际数据的以下页面请求中重复使用它们。


### RCurl login and browse private pages ###

library("RCurl")

loginurl ="http=//www.*****"
mainurl  ="http=//www.*****"
agent    ="Mozilla/5.0"

#User account data and other login pars
pars=list(
     RURL="http=//www.*****",
     Username="*****",
     Password="*****"
)

#RCurl pars     
curl = getCurlHandle()
curlSetOpt(cookiejar="cookiesk.txt",  useragent = agent, followlocation = TRUE, curl=curl)
#or simply
#curlSetOpt(cookiejar="", useragent = agent, followlocation = TRUE, curl=curl)

#post login form
web=postForm(loginurl, .params = pars, curl=curl)

#go to main url with real data
web=getURL(mainurl, curl=curl)

#parse/print content of web
#..... etc. etc.


#This has the side effect of saving cookie data to the cookiejar file 
rm(curl)
gc()

【讨论】:

    【解决方案3】:

    这是一种创建发布请求的方法,使用RCurl 保留和重用生成的 cookie,例如在需要身份验证时获取网页:

    library(RCurl)
    curl <- getCurlHandle()
    curlSetOpt(cookiejar="/tmp/cookies.txt", curl=curl)
    postForm("http://example.com/login", login="mylogin", passwd="mypasswd", curl=curl)
    getURL("http://example.com/anotherpage", curl=curl)
    

    【讨论】:

    • +1 有效。在关闭线程之前,让我们看看是否有人想用 httr 发布一些东西。
    • 根据我对文档的阅读,我不需要设置 CURLOPT_COOKIEJAR - 只需将它们写入磁盘。
    • @hadley 如果我不使用cookiejar,我将无法访问某些需要cookies 的站点。如果不需要实际文件,他/她可以使用curlSetOpt(cookiejar="", curl=curl)。无论如何,即使你传递了一个文件,除非你发出rm(curl); gc(),否则不会保存任何内容。
    猜你喜欢
    • 2015-06-24
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多