From: Ernest Ellingson Date: 2001-04-21T22:57:30+09:00 Subject: [ruby-talk:13978] Re: Net::HTTP basic authentication At 21:48 4/21/2001 +0900, you wrote: >Thanks!!! > >donald > >On Saturday 21 April 2001 08:39, ts wrote: > > >>>>> "d" == dsharp2@nc rr com writes: > > > > d> Does anyone have some pointers that would explain how this all works? > > > > RFC 2617 > > > > ftp://ftp.isi.edu/in-notes/rfc2617.txt > > > > > > Guy Decoux Try this for doing basic auth. require 'net/http' class Fetch attr_reader :headers, :data def initialize (loc, port, url,unpw=nil) @loc=loc @port=port @url=url @unpw=unpw @headers="" @data="" end def get() h=Net::HTTP.new(@loc,@port) begin unpwd=@unpw if @unpw z=[unpwd].pack("m") unpwd={'Authorization' => 'Basic ' + z } end resp, data = h.get(@url,unpwd) @headers="" resp.each {|key, val| @headers.concat("#{key}: #{val}\n")} @data=data rescue Net::ProtoFatalError => detail head = detail.data head.each {|key,val| puts "#{key} #{val}"} if head.code=="404" puts "File not Found" elsif head.code=="401" print "Authorization Required\n User name?" gets uname=$_.chomp print "Password?" gets pwd=$_.chomp @unpwd=uname + ":" + pwd hval=[unpw].pack("m") @unpwd={'Authorization' => 'Basic ' + hval} retry end rescue SystemCallError puts "Can't connect" end end end x=Fetch.new('loc', port, 'path','optional user:password') x.get puts x.headers puts x.data