1. 建立HTTP连接(通过GET方式发送请求参数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
require "open-uri" #如果有GET请求参数直接写在URI地址中 uri = 'http://uri' html_response = nil open(uri) do |http| html_response = http.read end puts html_response require "open-uri" #如果有GET请求参数直接写在URI地址中 uri = 'http://uri' html_response = nil open(uri) do |http| html_response = http.read end puts html_response |
2. 通过POST发送请求参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
params = {} params[ "name" ] = 'Tom' uri = URI .parse( "http://uri" ) res = Net:: HTTP .post_form (uri, params) #返回的cookie puts res.header[ 'set-cookie' ] #返回的html body puts res.body params = {} params[ "name" ] = 'Tom' uri = URI .parse( "http://uri" ) res = Net:: HTTP .post_form (uri, params) #返回的cookie puts res.header[ 'set-cookie' ] #返回的html body puts res.body |
3.HTTPS请求
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
|
# # 描述: # 发送快递数据到datasystem,使用https # 输入: # data - 组装后的expess的数据 # 输出: # datasystem返回的状态信息 # def self .senddatassl(url,data) url = url + data $logger .info(url) begin uri = URI .parse( URI .escape(url)) http = Net:: HTTP . new (uri.host, uri.port) http.use_ssl = true if ( $logger != nil ) $logger .info( "链接地址参数:#{URI.escape(url)},文件名:#{__FILE__},第 #{__LINE__} 行" ) $logger .info( "传入data参数:#{data.to_json},文件名:#{__FILE__},第 #{__LINE__} 行" ) end request = Net:: HTTP ::Get. new (uri.request_uri) response = http.request(request) rescue =>exception $logger .error( "传递url地址为#{url},错误!#{exception.to_s},文件名:#{__FILE__},第 #{__LINE__} 行" ) return nil end return response.body end |