=begin = net/http.rb == ���Υ饤�֥��ˤĤ��� ���ѥǡ���ž���ץ��ȥ��� HTTP �򰷤��饤�֥��Ǥ��� ������ [RFC2616] (()) �� �𤤤Ƥ��ޤ��� == ������ === �����֥����Ф���ɥ�����Ȥ����� (GET) require 'net/http' Net::HTTP.version_1_1 # declear to use 1.1 features. Net::HTTP.start( 'some.www.server', 80 ) {|http| response, body = http.get('/index.html') print body } �ޤ��ʲ���Ʊ����̣��û���񤤤���ΤǤ��� require 'net/http' Net::HTTP.get_print 'some.www.server', '/index.html' === �ե�����ξ������������ (POST) require 'net/http' Net::HTTP.version_1_1 # declear to use 1.1 features. Net::HTTP.start( 'some.www.server', 80 ) {|http| response, body = http.post( '/cgi-bin/any.rhtml', 'querytype=subject&target=ruby' ) } === �ץ�������ͳ�Υ������� Net::HTTP �Υ��饹�᥽�å� Net::HTTP.Proxy �ϡ���˥ץ�������ͳ�� ��³����褦��ư��򤹤롢���������饹����������֤��ޤ������Υ��饹�� Net::HTTP ��Ѿ����Ƥ���Τ� Net::HTTP ������Ʊ���褦�˻Ȥ��ޤ��� require 'net/http' Net::HTTP.version_1_1 # declear to use 1.1 features. $proxy_addr = 'your.proxy.addr' $proxy_port = 8080 : Net::HTTP::Proxy($proxy_addr, $proxy_port).start( 'some.www.server' ) {|http| # always connect to your.proxy.addr:8080 : } �ޤ� Net::HTTP.Proxy ���������� nil ���� Net::HTTP ���Ȥ��֤��Τ� ��Υ����ɤΤ褦�˽񤤤Ƥ����Хץ������ʤ��ξ��ˤ��б��Ǥ��ޤ��� === ������쥯�Ȥ��б����� require 'net/http' Net::HTTP.version_1_1 # declear to use 1.1 features. host = 'www.ruby-lang.org' path = '/' begin Net::HTTP.start( host, 80 ) {|http| response , = http.get(path) print response.body } rescue Net::ProtoRetriableError => err if m = %r.match( err.response['location'] ) then host = m[1].strip path = m.post_match retry end end ������Ǥ� URL ����ۥ���̾������Τˤ������������ˡ��ȤäƤ��ޤ����� ���� URI ���饹��ɸ��ź�դˤʤ�Ф�äȴ�ñ�ˤʤ�Ϥ��Ǥ��� === Basic ǧ�� require 'net/http' Net::HTTP.version_1_1 # declear to use 1.1 features. Net::HTTP.start( 'auth.some.domain' ) {|http| response, body = http.get( '/need-auth.cgi', 'Authorization' => 'Basic ' + ["#{account}:#{password}"].pack('m').strip ) print body } �С������ 1.2 (Ruby 1.7 �ʹߤ�ź��) �Ǥϼ��Τ褦�˽񤱤ޤ��� require 'net/http' Net::HTTP.version_1_2 # declear to use 1.2 features. req = Net::HTTP::Get.new('/need-auth.cgi') req.basic_auth 'account', 'password' Net::HTTP.start( 'auth.some.domain' ) {|http| response = http.request(req) print response.body } == ���������ͤؤ��ѹ��Ȱܹ����֤ˤĤ��� Ruby 1.6 �����äƤ���Τ� http.rb 1.1 �� 1.7 �ʹߤ� 1.2 �Ǥ����� ���δ֤ǤϤ��ʤ��礭�����ͤ��Ѥ��ޤ��������������˻��ͤ��ѹ� ����ΤǤʤ���ξ���μ�������¸������������ߤ��뤳�Ȥˤ��ޤ����� �᥽�å� HTTP.version_1_2��HTTP.version_1_1 ��Ƥ֤� ���Τ��Ȥ���������� Net::HTTP ���֥������ȤϤ��줾��� �С������λ��ͤ�ư���褦�ˤʤ�ޤ����ʲ��ϻ�����Ǥ��� # example Net::HTTP.start {|http1| ...(http1 has 1.2 features)... } Net::HTTP.version_1_1 Net::HTTP.start {|http2| ...(http2 has 1.1 features)... } Net::HTTP.version_1_2 Net::HTTP.start {|http3| ...(http3 has 1.2 features)... } ���ε�ǽ�ϥ���åɥ����դǤϤ���ޤ��� == class Net::HTTP === ���饹�᥽�å� : new( address, port = 80, proxy_addr = nil, proxy_port = nil ) ������ HTTP ���֥������Ȥ��������ޤ���address �� HTTP �����С��� FQDN �ǡ� port ����³����ݡ����ֹ�Ǥ������Υ᥽�åɤǤϤޤ���³�Ϥ��ޤ��� proxy_addr ��Ϳ����ȥץ�������𤷤���³���륪�֥������Ȥ��������ޤ��� : start( address, port = 80, proxy_addr = nil, proxy_port = nil ) : start( address, port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... } �ʲ���Ʊ���Ǥ��� Net::HTTP.new(address, port, proxy_addr, proxy_port).start(&block) : get( address, path, port = 80 ) �ۥ��� address �� port �֥ݡ��Ȥ���³���� path ��ɽ������ ����ƥ��ƥ��������ʸ������֤��ޤ��� : get_print( address, path, port = 80 ) �ۥ��� address �� port �֥ݡ��Ȥ���³���� path ��ɽ������ ����ƥ��ƥ����������������$stdout �� << �ǽ��Ϥ��ޤ��� : Proxy( address, port = 80 ) ��˻��ꤵ�줿�ץ���������³���륯�饹��������֤��ޤ��� ���Υ��饹�� Net::HTTP ��Ѿ����Ƥ���Τ� Net::HTTP ������ Ʊ���褦�˻Ȥ��ޤ��� address �� nil �ΤȤ��� Net::HTTP ���饹�򤽤Τޤ��֤��ޤ��� # example proxy_class = Net::HTTP::Proxy( 'proxy.foo.org', 8080 ) : proxy_class.start( 'www.ruby-lang.org' ) {|http| # connecting proxy.foo.org:8080 : } : proxy_class? ���Ȥ� (Proxy �᥽�åɤˤ�äƺ������줿) �ץ������ѤΥ��饹�ʤ�п��� : port HTTP �Υǥե���ȥݡ��� (80)�� === �᥽�å� : start : start {|http| .... } TCP ���ͥ�������ĥ�ꡢHTTP ���å����򳫻Ϥ��ޤ��� ���Ǥ˥��å���󤬳��Ϥ��Ƥ������㳰 IOError ��ȯ�����ޤ��� ���ƥ졼���Ȥ��ƸƤФ줿���ϥ֥��å��δ֤������å�������³���� �֥��å���λ�ȤȤ�˼�ưŪ�˥��å������Ĥ��ޤ��� : active? HTTP ���å���󤬳��Ϥ���Ƥ����鿿�� : address ��³���륢�ɥ쥹 : port ��³����ݡ����ֹ� : open_timeout : open_timeout=(n) ��³�����Ԥĺ����ÿ��������ÿ����äƤ⥳�ͥ������ �����ʤ�����㳰 TimeoutError ��ȯ�����ޤ��� : read_timeout : read_timeout=(n) �ɤߤ��� (read(1) ���) �ǥ֥��å����Ƥ褤�����ÿ��� �����ÿ����äƤ��ɤߤ���ʤ�����㳰 TimeoutError ��ȯ�����ޤ��� : finish HTTP ���å�����λ���ޤ������å���󳫻����ˤ��Υ᥽�åɤ� �ƤФ줿�����㳰 IOError ��ȯ�����ޤ��� : proxy? �ץ�������𤷤���³����ʤ鿿�� : proxy_address �ץ�������ͳ����³���� HTTP ���֥������Ȥʤ�ץ������Υ��ɥ쥹�� �����Ǥʤ��ʤ� nil�� : proxy_port �ץ�������ͳ����³���� HTTP ���֥������Ȥʤ�ץ������Υݡ��ȡ� �����Ǥʤ��ʤ� nil�� : get( path, header = nil ) : get( path, header = nil ) {|str| .... } �����о�� path �ˤ��륨��ƥ��ƥ���������ޤ����ޤ� header �� nil �Ǥʤ���С��ꥯ�����Ȥ�����Ȥ��ˤ������Ƥ� HTTP �إå��Ȥ��ƽ� ���ߤޤ���header �ϥϥå���ǡ��֥إå�̾ => ���ơפΤ褦�ʷ����� �ʤ���Ф����ޤ��� �֤��ͤϡ��С������ 1.1 �Ǥ� HTTPResponse �ȥ���ƥ��ƥ��ܥǥ�ʸ����� �����Ǥ�����Ǥ���1.2 �Ǥ� HTTPResponse �����ҤȤĤΤߤǤ������ξ�硢 ����ƥ��ƥ��ܥǥ��� response.body �������ޤ��� �֥��å��ȤȤ�˸ƤФ줿���ϥ���ƥ��ƥ��ܥǥ��򾯤��Ťĥ֥��å��� Ϳ���ޤ��� 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ�� HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ��� ���� 1.2 �Ǥ������㳰��ȯ�����ޤ��� # version 1.1 (bundled with Ruby 1.6) response, body = http.get( '/index.html' ) # version 1.2 (bundled with Ruby 1.7 or later) response = http.get( '/index.html' ) # compatible in both version response , = http.get( '/index.html' ) response.body # using block File.open( 'save.txt', 'w' ) {|f| http.get( '/~foo/', nil ) do |str| f.write str end } : head( path, header = nil ) �����о�� path �ˤ��륨��ƥ��ƥ��Υإå��Τߤ�������ޤ��� �ޤ� header �� nil �Ǥʤ���Хꥯ�����Ȥ�����Ȥ��ˤ������Ƥ� HTTP �إå��Ȥ��ƽ񤭤��ߤޤ���header �ϥϥå���ǡ� �֥إå�̾ => ���ơפΤ褦�ʷ����Ǥʤ���Ф����ޤ��� HTTPResponse ���֥������Ȥ��֤��ޤ��� 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ�� HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ��� ���� 1.2 �Ǥ������㳰��ȯ�����ޤ��� response = nil Net::HTTP.start( 'some.www.server', 80 ) {|http| response = http.head( '/index.html' ) } p response['content-type'] : post( path, data, header = nil ) : post( path, data, header = nil ) {|str| .... } �����о�� path �ˤ��륨��ƥ��ƥ����Ф�ʸ���� data �� ����ޤ����쥹�ݥ󥹤� << �᥽�åɤ�Ȥä� dest �˽� ���ޤ�ޤ���header �� get �᥽�åɤ�Ʊ���Ǥ��� HTTPResponse ���֥������Ȥ� dest ��������֤��ޤ��� ���ƥ졼���Ȥ��ƸƤӤ����줿�Ȥ��ϥ���ƥ��ƥ��ܥǥ��򾯤��Ť� �֥��å���Ϳ���ޤ��� 1.1 �Ǥ� 3xx (�ƻ�Բ�ǽ�ʥ��顼)���Ф��Ƥ��㳰��ȯ�����ޤ������ξ�� HTTPResponse ���㳰���֥������Ȥ��� err.response �����뤳�Ȥ��Ǥ��ޤ��� ���� 1.2 �Ǥ������㳰��ȯ�����ޤ��� # version 1.1 response, body = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' ) # version 1.2 response = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' ) # compatible in both version response , = http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' ) # using block File.open( 'save.html', 'w' ) {|f| http.post( '/cgi-bin/search.rb', 'query=subject&target=ruby' ) do |str| f.write str end } : request_get( path, header = nil ) : request_get( path, header = nil ) {|response| .... } path �ˤ��륨��ƥ��ƥ���������ޤ���HTTPResponse ���֥������Ȥ��֤��ޤ��� �֥��å��ȤȤ�˸ƤӽФ��줿�Ȥ��ϡ��֥��å��¹������³�� �ݻ������ޤ� HTTPResponse ���֥������Ȥ�֥��å����Ϥ��ޤ��� ���Υ᥽�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ��� # example response = http.request_get( '/index.html' ) p response['content-type'] puts response.body # body is already read # using block http.request_get( '/index.html' ) {|response| p response['content-type'] response.read_body do |str| # read body now print str end } : request_post( path, data, header = nil ) : request_post( path, data, header = nil ) {|response| .... } path �ˤ��륨��ƥ��ƥ���������ޤ���HTTPResponse ���֥������Ȥ��֤��ޤ��� �֥��å��ȤȤ�˸ƤӽФ��줿�Ȥ��ϡ��ܥǥ����ɤߤ������� HTTPResponse ���֥������Ȥ�֥��å����Ϥ��ޤ��� ���Υ᥽�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ��� # example response = http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) p response.status puts response.body # body is already read # using block http.post2( '/cgi-bin/nice.rb', 'datadatadata...' ) {|response| p response.status p response['content-type'] response.read_body do |str| # read body now print str end } == class Net::HTTPResponse HTTP �쥹�ݥ󥹤Υ��饹�Ǥ��� �������إå��ե������̾�Ǥ����硢��ʸ����ʸ������̤��ޤ��� === �᥽�å� : self[ key ] key �إå��ե������(ʸ����)�Ǥ������Ȥ��Х��� 'content-length' ���Ф��Ƥ� '2048' �Τ褦��ʸ���������ޤ��� key ����ʸ����ʸ������̤��ޤ��� : self[ key ] = val key �إå��ե�����ɤ� value �����ꤷ�ޤ��� key ����ʸ����ʸ������̤��ޤ��� : key?( key ) key �Ȥ����إå��ե�����ɤ�����п��� key ����ʸ����ʸ������̤��ޤ��� : each {|name,value| .... } ���٤ƤΥإå��ե������̾�Ȥ����ͤΥڥ����Ф��뤯�꤫������ : canonical_each {|name,value| .... } �إå��ե�����ɤ�����̾�Ȥ����ͤΥڥ����Ф��Ʒ����֤��ޤ��� : code HTTP �Υꥶ��ȥ����ɤǤ����㤨�� '302' �ʤɤǤ��� : message HTTP �����Ф��ꥶ��ȥ����ɤ��ղä����֤���å������Ǥ��� �㤨�� 'Not Found' �ʤɤǤ��� : read_body( dest = '' ) ����ƥ��ƥ��ܥǥ�������� dest �� << �᥽�åɤ�Ȥäƽ񤭤��ߤޤ��� Ʊ�� HTTPResponse ���֥������Ȥ��Ф������ʾ�ƤФ줿��硢 ����ܤ���Ϥʤˤ⤻���˰���ܤ��֤��ͤ򤽤Τޤ��֤��ޤ��� : read_body {|str| .... } ����ƥ��ƥ��ܥǥ��򾯤��Ťļ������ƽ缡�֥��å���Ϳ���ޤ��� : body ����ƥ��ƥ��ܥǥ��Ǥ���read_body ��Ƥ�Ǥ���Ф��ΰ��� dest�� �Ƥ�Ǥ��ʤ���Х���ƥ��ƥ��ܥǥ���ʸ����Ȥ����ɤߤ�����֤��ޤ��� =end