# Reading from a URL

**URL:** <https://rubytalk.org/t/reading-from-a-url/2187>\
**Category:** ruby-talk\
**Created:** [7 October 2002 17:33 UTC](https://rubytalk.org/t/reading-from-a-url/2187 "2002-10-07T17:33:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Volkmann\_Mark](https://avatars.discourse-cdn.com/v4/letter/v/67e7ee/32.png) [@Volkmann\_Mark](https://rubytalk.org/u/Volkmann_Mark)\
**Post date:** [7 October 2002 17:33 UTC](https://rubytalk.org/t/reading-from-a-url/2187/1 "2002-10-07T17:33:52Z")

</div>

I need to read the content of an HTTP URL. I can break the URL up into  
server, port and path and use Net::HTTP to read it, but it seems like there  
should be an easier way. I’d really like to pass the URL string to a method  
of some class that would return an object from a class that inherits from  
IO. Does something like that exist? If not, is there an easier way than  
using Net::HTTP?

> **···**
>
> * * *
> 
> WARNING: All e-mail sent to and from this address will be received or  
> otherwise recorded by the A.G. Edwards corporate e-mail system and is  
> subject to archival, monitoring or review by, and/or disclosure to,  
> someone other than the recipient.
> 
> * * *

---

<div class="post-metadata">

**Author:** ![GOTO\_Kentaro1](https://avatars.discourse-cdn.com/v4/letter/g/e5b9ba/32.png) [@GOTO\_Kentaro1](https://rubytalk.org/u/GOTO_Kentaro1)\
**Post date:** [7 October 2002 18:22 UTC](https://rubytalk.org/t/reading-from-a-url/2187/2 "2002-10-07T18:22:07Z")

</div>

If you have Ruby 1.7, try

require “net/http”  
require “uri”  
content = Net::HTTP.get\_print(URI.parse(“[Example Domain](http://www.example.com/index.html)”))

If not,

require “net/http”  
require “uri”  
uri = URI.parse(“[Example Domain](http://www.example.com/index.html)”)  
content = Net::HTTP.get(uri.host, uri.request\_uri, uri.port)

Read net/http.rb if you would need http auth, redirection or proxy server.  
This library includes the documentation of itself.

Or try WebFetcher in RAA.

Gotoken

> **···**
>
> At Tue, 8 Oct 2002 02:33:52 +0900, Volkmann, Mark [Mark.Volkmann@AGEDWARDS.com](mailto:Mark.Volkmann@AGEDWARDS.com) wrote:
> 
> > I need to read the content of an HTTP URL. I can break the URL up into  
> > server, port and path and use Net::HTTP to read it, but it seems like there  
> > should be an easier way. I’d really like to pass the URL string to a method  
> > of some class that would return an object from a class that inherits from  
> > IO. Does something like that exist? If not, is there an easier way than  
> > using Net::HTTP?

---

<div class="post-metadata">

**Author:** ![Alexander\_Bokovoy](https://avatars.discourse-cdn.com/v4/letter/a/ecb155/32.png) [@Alexander\_Bokovoy](https://rubytalk.org/u/Alexander_Bokovoy)\
**Post date:** [8 October 2002 09:54 UTC](https://rubytalk.org/t/reading-from-a-url/2187/3 "2002-10-08T09:54:50Z")

</div>

BTW, current Net::HTTP code does not support proxies with authentication  
enabled which makes it rather unusable in corporate networks. I’ve sent a  
patch to fix it to Matz and he forwarded it to Minero Anoki (net/http.rb  
maintainer) but fix still isn’t in CVS.

I’m attaching it here in meantime.

[ruby-net-http-alt.patch](https://rubytalk.org/uploads/short-url/abDPnqdLrTbkdCRYFiGyN0ZsGY9.patch) (5.82 KB)

> **···**
>
> On Tue, Oct 08, 2002 at 03:22:07AM +0900, GOTO Kentaro wrote:
> 
> > At Tue, 8 Oct 2002 02:33:52 +0900, \> Volkmann, Mark [Mark.Volkmann@AGEDWARDS.com](mailto:Mark.Volkmann@AGEDWARDS.com) wrote:
> > 
> > > I need to read the content of an HTTP URL. I can break the URL up into  
> > > server, port and path and use Net::HTTP to read it, but it seems like there  
> > > should be an easier way. I’d really like to pass the URL string to a method  
> > > of some class that would return an object from a class that inherits from  
> > > IO. Does something like that exist? If not, is there an easier way than  
> > > using Net::HTTP?
> > 
> > If you have Ruby 1.7, try
> > 
> > require “net/http”  
> > require “uri”  
> > content = Net::HTTP.get\_print(URI.parse(“[Example Domain](http://www.example.com/index.html)”))
> > 
> > If not,
> > 
> > require “net/http”  
> > require “uri”  
> > uri = URI.parse(“[Example Domain](http://www.example.com/index.html)”)  
> > content = Net::HTTP.get(uri.host, uri.request\_uri, uri.port)
> > 
> > Read net/http.rb if you would need http auth, redirection or proxy server.  
> > This library includes the documentation of itself.  
> > –  
> > / Alexander Bokovoy
> 
> * * *
> 
> Q: What is the sound of one cat napping?  
> A: Mu.

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Mirra4](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Massimiliano\_Mirra4](https://rubytalk.org/u/Massimiliano_Mirra4)\
**Post date:** [9 October 2002 15:53 UTC](https://rubytalk.org/t/reading-from-a-url/2187/4 "2002-10-09T15:53:58Z")

</div>

Is there a std lib way that works both for 1.6 and 1.7?

Massimiliano

> **···**
>
> On Tue, Oct 08, 2002 at 03:22:07AM +0900, GOTO Kentaro wrote:
> 
> > > IO. Does something like that exist? If not, is there an easier way than  
> > > using Net::HTTP?
> > 
> > If you have Ruby 1.7, try
> > 
> > require “net/http”  
> > require “uri”  
> > content = Net::HTTP.get\_print(URI.parse(“[Example Domain](http://www.example.com/index.html)”))
> > 
> > If not,
> > 
> > require “net/http”  
> > require “uri”  
> > uri = URI.parse(“[Example Domain](http://www.example.com/index.html)”)  
> > content = Net::HTTP.get(uri.host, uri.request\_uri, uri.port)
> > 
> > Read net/http.rb if you would need http auth, redirection or proxy server.  
> > This library includes the documentation of itself.
> > 
> > Or try WebFetcher in RAA.
