1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
=begin
= net/http.rb
== ���Υ饤�֥��ˤĤ���
���ѥǡ���ž���ץ��ȥ��� HTTP version 1.1 ���饤�֥��Ǥ���
������ [RFC2616] ((<URL:http://www.ietf.org/rfc/rfc2616.txt>)) ��
�𤤤Ƥ��ޤ���
== ������
=== �����֥����Ф���ɥ�����Ȥ����� (GET)
require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http|
response = http.get('/index.html')
puts response.body
}
�ޤ��ʲ���Ʊ����̣��û������ΤǤ���
require 'net/http'
Net::HTTP.get_print 'some.www.server', '/index.html'
# or
Net::HTTP.get_print URI.parse('http://www.example.com/index.html')
=== �ե�����ξ������������ (POST)
require 'net/http'
Net::HTTP.start( 'some.www.server', 80 ) {|http|
response = http.post('/cgi-bin/any.rhtml',
'querytype=subject&target=ruby')
}
=== �ץ�������ͳ�Υ�������
Net::HTTP �Υ��饹��å� Net::HTTP.Proxy �ϡ���˥ץ�������ͳ��
��³����褦��ư��롢���������饹����������֤��ޤ������Υ��饹��
Net::HTTP ��Ѿ����Ƥ���Τ� Net::HTTP ������Ʊ���褦�˻Ȥ��ޤ���
require 'net/http'
$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'
def read_uri( uri_str )
response = Net::HTTP.get_response(URI.parse(uri_str))
case response
when Net::HTTPSuccess then response
when Net::HTTPRedirection then read_uri(response['location'])
else
response.error!
end
end
print read_uri('http://www.ruby-lang.org')
HTTPSuccess �� HTTPRedirection �� HTTPResponse ���饹�β��̥��饹�Ǥ���
HTTPResponse ���֥������ȤϤ��줾�� HTTP �쥹�ݥΥ��ơ������ˤ�ä�
�ۤ륯�饹��°���Ƥ��ꡢ���Υ��饹�Ƿ�̤�ʬ��Ǥ��ޤ����ɤΤ褦�ʥ��饹��
�Ѱդ���Ƥ���Τ��ˤĤ��Ƥϡ�HTTP �쥹�ݥ��饹���פ���Ƥ���������
=== Basic ǧ��
require 'net/http'
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
}
=== HTTP �쥹�ݥ��饹��
�ʲ��� HTTP 1.1 �Υꥶ��ȥ����ɤȤ�����б�����쥹�ݥ��饹��
�����ޤ������饹�Ϥ��٤� Net �⥸�塼�������������Ƥ��ꡢ
����ǥ�Ȥ��Ѿ��ط���ɽ�路�Ƥ��ޤ���
xxx HTTPResponse
1xx HTTPInformation
100 HTTPContinue
101 HTTPSwitchProtocol
2xx HTTPSuccess
200 HTTPOK
201 HTTPCreated
202 HTTPAccepted
203 HTTPNonAuthoritativeInformation
204 HTTPNoContent
205 HTTPResetContent
206 HTTPPartialContent
3xx HTTPRedirection
300 HTTPMultipleChoice
301 HTTPMovedPermanently
302 HTTPFound
303 HTTPSeeOther
304 HTTPNotModified
305 HTTPUseProxy
307 HTTPTemporaryRedirect
4xx HTTPClientError
400 HTTPBadRequest
401 HTTPUnauthorized
402 HTTPPaymentRequired
403 HTTPForbidden
404 HTTPNotFound
405 HTTPMethodNotAllowed
406 HTTPNotAcceptable
407 HTTPProxyAuthenticationRequired
408 HTTPRequestTimeOut
409 HTTPConflict
410 HTTPGone
411 HTTPLengthRequired
412 HTTPPreconditionFailed
413 HTTPRequestEntityTooLarge
414 HTTPRequestURITooLong
415 HTTPUnsupportedMediaType
416 HTTPRequestedRangeNotSatisfiable
417 HTTPExpectationFailed
5xx HTTPServerError
500 HTTPInternalServerError
501 HTTPNotImplemented
502 HTTPBadGateway
503 HTTPServiceUnavailable
504 HTTPGatewayTimeOut
505 HTTPVersionNotSupported
xxx HTTPUnknownResponse
== ���������ͤؤ��ѹ��Ȱܹ����֤ˤĤ���
Ruby 1.6 �ˤ� http.rb 1.1��Ruby 1.7 �ˤ� http.rb 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 )
������ Net::HTTP ���֥������Ȥ������� HTTP ���å�����
���Ϥ����������֤��ޤ���
: start( address, port = 80, proxy_addr = nil, proxy_port = nil ) {|http| .... }
������ Net::HTTP ���֥������Ȥ��������֥��å����Ϥ��ޤ���
�֥��å��¹���Τ� HTTP ���å�����ݻ����ޤ���
�֥��å����֤��ͤΤޤ��֤��ޤ���
: get_print( uri )
: get_print( address, path, port = 80 )
uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
������ stdout �˽��Ϥ��ޤ���
Net::HTTP.get_print URI.parse('http://www.example.com')
: get( uri )
: get( address, path, port = 80 )
uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
������ʸ������֤��ޤ���
print Net::HTTP.get(URI.parse('http://www.example.com'))
: get_response( uri )
: get_response( address, path, port = 80 )
uri �ޤ��� address path port �ǻ��ꤵ�줿����ƥ��ƥ���
������ Net::HTTPResponse ���֥������Ȥ��֤��ޤ���
res = Net::HTTP.get_response(URI.parse('http://www.example.com'))
print res.body
: 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 ��ȯ�����ޤ���
���ƥ졼���Ȥ��ƸƤФ줿���ϥ֥��å��δ֤������å�����
�ݻ������֥��å���λ�ȤȤ�˼�ưŪ�˥��å�����λ���ޤ���
: started?
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
}
: request( request [, data] )
: request( request [, data] ) {|response| .... }
HTTPResquest ���֥������� request ���������ޤ���POST/PUT �λ��� data ��
Ϳ�����ޤ� (POST/PUT �ʳ��� data ��Ϳ����� ArgumentError ��ȯ�����ޤ�)��
�֥��å��ȤȤ�˸ƤӤ����줿�Ȥ��ϥܥǥ����ɤߤ��ޤ��� HTTPResponse
���֥������Ȥ�֥��å���Ϳ���ޤ���
���Υ�åɤ� HTTP �ץ��ȥ���˴�Ϣ�����㳰��ȯ�������ޤ���
== class Net::HTTPRequest
HTTP �ꥯ�����Ȥ���ݲ����륯�饹���ºݤˤϲ��̥��饹��
Net::HTTP::Get, Post, Head ��Ȥ��ޤ���
=== ���饹��å�
: new
HTTP �ꥯ�����ȥ��֥������Ȥ��������ޤ���
=== ��å�
: self[ key ]
key �إå��ե�����ɤ�ʸ����
key ����ʸ����ʸ������̤��ޤ���
: self[ key ] = val
key �إå��ե�����ɤ� val �åȤ��ޤ���
key ����ʸ����ʸ������̤��ޤ���
: each {|name, val| .... }
�إå�̾�Ȥ����ͤ��Ф��뤯�꤫�������إå�̾�Ͼ�ʸ�������줵��ޤ���
: basic_auth( account, password )
Authrization: �إå��� basic auth �Ѥ˥��åȤ��ޤ���
: range
Range: �إå��μ����ϰϤ� Range ���֥������Ȥ��֤��ޤ���
: range = r
: set_range( i, len )
�ϰϤ���ꤷ�ƥ���ƥ��ƥ���������뤿��Υإå� Range: �åȤ��ޤ���
r �� Range ���֥������ȡ�i, len �ϻ�����Ĺ���Ǥ���
: content_length
Content-Length: �إå����� (����)��
: content_range
Content-Range: �إå����� (Range)��
== 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
|