使用cURL,RCurl和httr的Cookie发送请求 [英] Post request using cookies with cURL, RCurl and httr

查看:243
本文介绍了使用cURL,RCurl和httr的Cookie发送请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  curl --dump-header cook.txt ^ 
--dataRURL = http = // www.example.com/r&user=bob&password=hello^
--user-agentMozilla / 5.0^
http://www.example.com/login

使用键入cook。 txt 我得到类似这样的回应:

  HTTP / 1.1 302 Found 
Date :Thu,******
服务器:Microsoft-IIS / 6.0
服务器:******
X-Powered-By:ASP.NET
X -AspNet-Version:1.1.4322
Location:******
Set-Cookie:Cookie1 =; domain = ******; expires = ****** ******
******
******
缓存控制:private
Content-Type :text / html; charset = iso-8859-1
Content-Length:189

线如: Set-Cookie:AuthCode = ABC ... (我可以脚本这个当然)。因此,我可以使用 AuthCode 来处理后续请求。



我想用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] 
状态:411
Content-type:text / html
< h1>长度必需< / h1>

总的来说我知道411错误,我可以尝试修复请求;但我不能在cURL中,所以我做错了POST命令。



你能帮我把我的cURL命令转换为RCurl和/或httr ?

解决方案

根据Juba的建议,这里是一个工作的RCurl模板。



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


  1. 登录屏幕和

  2. 在包含实际数据的以下页面请求中重复使用它们。


  ### RCurl登录和浏览私人网页### 

库(RCurl)

loginurl =http = // www。*****
mainurl =http = // www。*****
agent =Mozilla / 5.0

#User帐户数据和其他登录参数
pars = list(
RURL =http = // www。*****,
用户名=*****,
Password =*****


#RCurl pars
curl = getCurlHandle()
curlSetOpt(cookiejar =cookiesk.txt,useragent = agent,followlocation = TRUE,curl = curl )
#或简单的
#curlSetOpt(cookiejar =,useragent = agent,followlocation = TRUE,curl = curl)

#post登录表单
web = postForm(loginurl,.params = pars,curl = curl)

#go到真实数据的主URL
web = getURL(mainurl,curl = curl)
$ b b#parse /打印web
#.....的内容等等。


#这会产生将Cookie数据保存到cookiejar文件的副作用
rm(curl)
gc()


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=hello" ^
  --user-agent  "Mozilla/5.0"  ^
  http://www.example.com/login

With type cook.txt I get a response similar to this:

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

I can manually read cookie lines like: Set-Cookie: AuthCode=ABC... (I could script this of course). So I can use AuthCode for subsequent requests.

I am trying do the same in R with RCurl and/or httr (still don't know which one is better for my task).

When I try:

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"))  

I get a response similar to this:

Response [http://www.example.com/error]
  Status: 411
  Content-type: text/html
<h1>Length Required</h1> 

By and large I know about 411-error and I could try to fix the request; but I do not get it in cURL, so I am doing something wrong with the POST command.

Can you help me in translating my cURL command to RCurl and/or httr?

解决方案

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

这篇关于使用cURL,RCurl和httr的Cookie发送请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆