From: "Bob C." Date: 2011-01-22T09:04:26+09:00 Subject: Re: Using net/HTTP - How to login and then retrieve data? Yes, POST also requires basic_auth. No, a cookie is not required. For now I'd like to stick with using net/http, and possibly later look at things like httpclient. When I code according to your example (see below) I am still failing on the SEARCH operation due to error . This doesn't make sense because I have successfully logged in, and I am re-using the connection. I appreciate your help Rob, because I didn't know how to re-use the connection - Thanks! --Bob Here's my current code: require 'net/http' url1 = URI.parse('http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/login') http = Net::HTTP.new(url1.host, url1.port) http.read_timeout = 120 http.start do |connection| #LOGIN request1 = Net::HTTP::Post.new(url1.request_uri) request1.basic_auth 'myuser', 'mypass' request1.set_form_data({'RETS-Version' => 'RETS/1.7', 'Accept' => '*/*', 'User-Agent' => 'Mozilla/4.0'}) #request1.body = 'something if needed' response1 = connection.request(request1) puts response1.body #SEARCH url2 = URI.parse('http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/search') request2 = Net::HTTP::Post.new(url2.request_uri) request2.basic_auth 'myuser', 'mypass' request2.set_form_data({'RETS-Version' => 'RETS/1.7', 'Accept' => '*/*', 'User-Agent' => 'Mozilla/4.0', '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' }) response2 = connection.request(request2) puts response2.inspect #LOGOUT url3 = URI.parse('http://imls.rets.fnismls.com/rets/fnisrets.aspx/imls/logout') request3 = Net::HTTP::Post.new(url3.request_uri) request3.set_form_data({'RETS-Version' => 'RETS/1.7', 'Accept' => '*/*', 'User-Agent' => 'Mozilla/4.0'}) request3.basic_auth 'myuser', 'mypass' response3 = connection.request(request3) puts response3.body end # # # Rob Biedenharn wrote in post #976647: > On Jan 21, 2011, at 2:20 PM, Bob C. wrote: > >> to take place for a successful Login-POST-Logout. >> >> #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 > > >> url2 = URI.parse(url2Str) >> } >> 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/ -- Posted via http://www.ruby-forum.com/.