# How to read HTML from a password-protected page?

**URL:** <https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184>\
**Category:** Rails\
**Created:** [November 14, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184 "2007-11-14T17:16:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 14, 2007, 5:16pm UTC](https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184/1 "2007-11-14T17:16:49Z")

</div>

Hello,

I have a method that gets the html off a page. I give it a _word_ to  
look up, and it returns the results. I just switched to read from a  
password-protected page, and I can’t figure out how to send along the  
password so I can get authorized to read the page. Anyone know how to  
do this?

def getWordResults(word)  
require ‘open-uri’  
begin  
@page = open(“[http://joe.com/find?word=#{word}&lb=1&user=joe](http://joe.com/find?word=#%7Bword%7D&lb=1&user=joe)”).read  
rescue # if page won’t open  
nil  
end  
end

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 14, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184/2 "2007-11-14T18:22:08Z")

</div>

Joe P. wrote:

> Hello,
> 
> I have a method that gets the html off a page. I give it a _word_ to  
> look up, and it returns the results. I just switched to read from a  
> password-protected page, and I can’t figure out how to send along the  
> password so I can get authorized to read the page. Anyone know how to  
> do this?
> 
> def getWordResults(word)  
> require ‘open-uri’  
> begin  
> @page = open(“[http://joe.com/find?word=#{word}&lb=1&user=joe](http://joe.com/find?word=#%7Bword%7D&lb=1&user=joe)”).read  
> rescue # if page won’t open  
> nil  
> end  
> end  
> If it helps at all, I can get to the page using a browser and manually  
> entering the user and password. Does anyone know how to do that  
> automatically?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 14, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184/3 "2007-11-14T18:45:50Z")

</div>

If it is using http authentication, you can use the format of  
[http://user:removed\_email\_address@domain.invalid?restofurl](http://user:removed_email_address@domain.invalid?restofurl)

* * *

Chris

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [November 14, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/how-to-read-html-from-a-password-protected-page/120184/4 "2007-11-14T20:25:26Z")

</div>

On Nov 14, 2007, at 12:45 PM, Chris E. wrote:

> > > Hello,  
> > > def getWordResults(word)  
> > > entering the user and password. Does anyone know how to do that  
> > > automatically?

You can use httpclient for this kind of thing:

require ‘rubygems’  
require ‘httpclient’

url = “[http://joe.com/find?word=#{word}&lb=1&user=joe](http://joe.com/find?word=#%7Bword%7D&lb=1&user=joe)”  
client = HTTPClient.new  
client.debug\_dev = STDOUT if $DEBUG  
client.set\_auth(url, ‘joe’, ‘thePassw0rd’)  
resp = client.get(url)  
@page = resp.content

You might have to make the first arg to .set\_auth be just the URL  
without the query string.

Also, if the service doesn’t send back an authorization request (which  
it sounds like it does since the browser asks you), then HTTPClient  
won’t volunteer the credentials you’ve provided.

You can check things like if resp.status != 200

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
