From: Rob Biedenharn Date: 2011-01-22T04:58:56+09:00 Subject: Re: Using net/HTTP - How to login and then retrieve data? On Jan 21, 2011, at 2:20 PM, Bob C. wrote: > I'm new to Ruby, coming from Java. I need to write an HTTP client in > Ruby. I'm working in a Windows environment with Ruby 1.8 and RubyMine. > > I've been working with net/http, and have written the code below, to > login to an HTTP server, then issue a POST to pull over some data, and > then logout. I can successfully Login, but the subsequent POST returns > an error indicating that I have Invalid Username/Password. I am also > new > to HTTP processing, so I am not sure of the sequence of events that > need > to take place for a successful Login-POST-Logout. > > Please review my code and let me know what I need to do for the POST > to > work. > > Thanks! > > --Bob > > require 'net/http' > > #LOGIN > url1 = > URI.parse('http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/ > login') > req = Net::HTTP::Get.new(url1.path) > req.basic_auth 'myuser', 'mypass' Do you need to also set .basic_auth on the POST request? Do you need to store a cookie? (In which case, you might prefer using httpclient gem rather than net/http directly.) You can also do: http = Net::HTTP.new(url.host, url.port) http.read_timeout = 120 if url.scheme == 'https' || url.port == 443 http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http.timeout = 120 end http.start do |connection| request = Net::HTTP::Post.new(url.request_uri) request.basic_auth 'user', 'password' request.body = 'something if needed' response = connection.request(request) # do other stuff with the same connection... end > req.set_form_data({"RETS-Version" => "RETS/1.7", 'Accept' => '*/*', > 'User-Agent' => 'Mozilla/4.0'}) > res1 = Net::HTTP.start(url1.host, url1.port) {|http| > http.request(req)} > puts res1.body # This returns a 200 response and a listing of methods > that can be called, therefore this Login snippet works > > #POST > url2Str = 'http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/ > search' > url2 = URI.parse(url2Str) > path = 'http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/search' > http = Net::HTTP.new(url2.host, 80) > data = > 'SearchType=Property&Limit=15000&Format=COMPACT&Query=%28L_UpdateDate > %3D2011-01-03T23:49:57%2B%29,%28L_UpdateDate%3D2011-01-04T23:49:57%2D > %29&Class=BC_4' > headers = { > 'Accept' => '*/*', > 'User-Agent' => 'Mozilla/4.0', > 'RETS-Version' => 'RETS/1.7' > } > res2 = http.post(url2Str, data, headers) > puts res2.body # This returns error Invalid > Username/Password combination readbody=true> > > #LOGOUT > url3 = > URI.parse('http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/logout') > req = Net::HTTP::Get.new(url3.path) > req.basic_auth 'myuser', 'mypass' > req.set_form_data({"RETS-Version" => "RETS/1.7", 'Accept' => '*/*', > 'User-Agent' => 'Mozilla/4.0'}) > res3 = Net::HTTP.start(url3.host, url3.port) {|http| > http.request(req)} > puts res3.body # This returns a 200 response > > -- > Posted via http://www.ruby-forum.com/. > Rob Biedenharn Rob@AgileConsultingLLC.com http://AgileConsultingLLC.com/ rab@GaslightSoftware.com http://GaslightSoftware.com/