使用带有cURL,RCurl和httr的cookie发布请求

时间:2013-02-21 11:14:17

标签: r curl libcurl rcurl httr

在Windows cURL中,我可以发布类似于此的Web请求:

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用于后续请求。

我正在尝试使用RCurl和/或httr在R中执行相同操作(仍然不知道哪一个更适合我的任务)。

当我尝试:

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吗?

3 个答案:

答案 0 :(得分:2)

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,因此请在multipart = FALSE来电中使用POST。< / p>

答案 1 :(得分:2)

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

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

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

  3. ### 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()
    

答案 2 :(得分:1)

以下是一种创建帖子请求的方法,保留并重复使用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)
相关问题