R httr POST登录请求 [英] R httr POST request for signing in

查看:274
本文介绍了R httr POST登录请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R httr POST请求登录bondora.com,因为此站点似乎未在使用身份验证:

I'm trying to log into bondora.com with using R httr POST request, because this site does not appear to be using authentication:

library(httr)
login <- "https://www.bondora.com/en/login"
pars <- list(
    username = "MyUserName",
    password = "MyPassword"
    )
POST(login, body = pars)

登录后,该网站将用户定向回到登录页面bondora.com/en/home,但是,如果我解析POST请求,则该页面的标题与登录页面的标题相同:

After logging in, the website directs the user back to landing page bondora.com/en/home, but instead if I parse the the POST request I get the same title for the page as the one from the login page:

library(XML)
test <- POST(login, body = pars)
test <- content(test, as = "text")
parsedHtml <- htmlParse(test, asText = TRUE)
xpathSApply(parsedHtml, "//title", xmlValue)
[1] "Join or log in|Loans and investing|Bondora"

我尝试在其他一些站点上使用相同的技术,但除此站点外,它似乎运行得很好. POST命令的输出如下:

I tried using the same techinque on some other sites and it appeared to be working quite fine, except for this site. The output from the POST command is as follows:

POST(login, body = pars)
   Response [https://www.bondora.com/en/login]
      Status: 200
      Content-type: text/html; charset=utf-8
   <!DOCTYPE HTML>
   <html xmlns="http://www.w3.org/1999/xhtml">

...

我应该使用某些特定设置登录bondora.com/en/login吗?

Are there some specific settings I should use for logging in on to bondora.com/en/login?

更新1 根据@hadley的评论,我很累设置TRUE和FALSE的多部分,但没有帮助.然后,我通过浏览器检查了请求,并添加了相同的标头:

UPDATE 1 As per @hadley comment, I tired setting multipart TRUE and FALSE, but no help. Then I inspected the request via browser and added the same header:

login <- "https://www.bondora.com/en/authenticate"
pars <- list(
  username = "username",
  password = "password"
  )
headers <- list(
  "User-Agent" = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0",
  "Referer" = "https://www.bondora.com/en/login?returnurl=/en/home",
  "Host" = "www.bondora.com",
  "Connection" = "keep-alive",
  "Accept-Language" = "en-US,en;q=0.5",
  "Accept-Encoding" = "gzip, deflate",
  "Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
  )
POST(login, body = pars, add_headers(.headers = character(headers)))
Error in character(headers) : invalid 'length' argument

似乎我需要像HTML错误411所示指定长度参数.我该怎么办?我还尝试将Content-Length = 9844添加到请求标头中,就像在响应标头中一样,但也没有成功.

It seems that I would need to specify the length argument as did the HTML Error 411 indicate. How should I do that? I also tried adding Content-Length = 9844 to the Request Header as it was in the Response Header, but no success either.

推荐答案

我能够通过将httr_0.4升级到httr_0.5来解决此问题

I was able to solve this by upgrading httr_0.4 to httr_0.5

这篇关于R httr POST登录请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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