ruby 访问新浪微博API post方式和get方式

 1   require 'net/https'
 2   require 'uri'
 3 
 4   def post_api(api, args)
 5     uri = URI.parse api
 6     http = Net::HTTP.new(uri.host, uri.port)
 7     http.use_ssl = true
 8     req = Net::HTTP::Post.new(uri.request_uri)
 9     req.set_form_data(args)
10     response = http.request(req)
11     JSON.load(response.body)
12   end
13 
14   def get_api(api, args)
15     uri = URI.parse api
16     uri.query = args.collect { |a| "#{a[0]}=#{URI::encode(a[1].to_s)}" }.join('&')
17     http = Net::HTTP.new(uri.host, uri.port)
18     http.use_ssl = true
19     req = Net::HTTP::Get.new(uri.request_uri)
20     response = http.request(req)
21     JSON.load(response.body)
22   end

 

posted @ 2013-12-17 13:54  芬奇  阅读(1725)  评论(0编辑  收藏  举报