Post request using cookies with cURL, RCurl and httr

后端 未结 3 672
陌清茗
陌清茗 2021-02-06 16:23

In Windows cURL I can post a web request similar to this:

curl  --dump-header cook.txt ^
  --data \"RURL=http=//www.example.com/r&user=bob&password=hell         


        
3条回答
  •  执笔经年
    2021-02-06 16:54

    Based on Juba suggestion, here is a working RCurl template.

    The code emulates a browser behaviour, as it:

    1. retrieves cookies on a login screen and
    2. reuses them on the following page requests containing the actual data.


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

提交回复
热议问题