From b6b8750a29313a817c34cad5cdf0eaeb66fee2a4 Mon Sep 17 00:00:00 2001 From: aamine Date: Wed, 20 Apr 2005 16:53:27 +0000 Subject: * lib/net/http.rb: add rdoc. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8368 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/http.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 13 deletions(-) (limited to 'lib/net/http.rb') diff --git a/lib/net/http.rb b/lib/net/http.rb index dd4c6b3140..5f40a47602 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -46,31 +46,66 @@ module Net # :nodoc: # # === Getting Document From WWW Server # - # (formal version) + # Example #1: Simple GET+print # # require 'net/http' - # Net::HTTP.start('www.example.com', 80) {|http| - # response = http.get('/index.html') - # puts response.body - # } + # Net::HTTP.get_print 'www.example.com', '/index.html' # - # (shorter version) + # Example #2: Simple GET+print by URL # # require 'net/http' - # Net::HTTP.get_print 'www.example.com', '/index.html' + # require 'uri' + # Net::HTTP.get_print URI.parse('http://www.example.com/index.html') # - # or + # Example #3: More generic GET+print # # require 'net/http' # require 'uri' - # Net::HTTP.get_print URI.parse('http://www.example.com/index.html') + # + # url = URI.parse('http://www.example.com/index.html') + # res = Net::HTTP.start(url.host, url.port) {|http| + # http.get('/index.html') + # } + # puts res.body + # + # Example #4: More generic GET+print + # + # require 'net/http' + # + # url = URI.parse('http://www.example.com/index.html') + # req = Net::HTTP::Get.new(url.path) + # res = Net::HTTP.start(url.host, url.port) {|http| + # http.request(req) + # } + # puts res.body # # === Posting Form Data # # require 'net/http' - # Net::HTTP.start('some.www.server', 80) {|http| - # response = http.post('/cgi-bin/search.rb', 'query=ruby') - # } + # require 'uri' + # + # #1: Simple POST + # res = Net::HTTP.post_form(URI.parse('http://www.example.com/search.cgi'), + # {'q'=>'ruby', 'max'=>'50'}) + # puts res.body + # + # #2: POST with basic authentication + # res = Net::HTTP.post_form(URI.parse('http://jack:pass@www.example.com/todo.cgi'), + # {'from'=>'2005-01-01', 'to'=>'2005-03-31'}) + # puts res.body + # + # #3: Detailed control + # url = URI.parse('http://www.example.com/todo.cgi') + # req = Net::HTTP::Post.new(url.path) + # req.basic_auth 'jack', 'pass' + # req.set_form_data({'from'=>'2005-01-01', 'to'=>'2005-03-31'}, ';') + # res = Net::HTTP.new(url.host, url.port).start { http.request(req) } + # case res + # when Net::HTTPSuccess, Net::HTTPRedirection + # # OK + # else + # res.error! + # end # # === Accessing via Proxy # @@ -1467,7 +1502,7 @@ module Net # :nodoc: end - class HTTP + class HTTP # reopen class Get < HTTPRequest METHOD = 'GET' REQUEST_HAS_BODY = false -- cgit v1.2.3