开发者

How to access a web service that requires authetication [duplicate]

开发者 https://www.devze.com 2023-03-14 09:09 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Reading information from a password protected site
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Reading information from a password protected site

I have a webservice that provides data in csv form, based on the url you use to access it. i.e. http://sever.com/parameter1 returns a csv for parameter 1, http://sever.com/parameter1 returns a csv for parameter 2, etc. When I first access the site in my browser, I type in a username and password and can then access any data I want.

The problem arises when I try to import that data into R. I tried this function:

readLines('http://sever.com/parameter1')

But got the following error:

Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") : cannot open: HTTP status was '401 Unauthorized'

Of course, this is because R doesn't know to pass my username and password along with the request. How do I define these additional parameters in R? Is there any way to add a cookie to the request or something?

Thank you.

/edit: The开发者_如何学JAVA answer here (different question wording wasn't picked up by SO) worked for me:

Reading information from a password protected site

If anyone else has any other advice, please let me know.


Why don't you use curl to grab the file? That way you can set http headers for username and password:

curl --user name:password http://www.example.com

There is a curl library for R

http://curl.haxx.se/libcurl/r/
0

精彩评论

暂无评论...
验证码 换一张
取 消