构建网址
uri = URI.parse("http://www.baidu.com")
构建数据
param = {
:apikey => 'f6e649ec8b49bf3c9b6110',
:mobile => phone,
:tpl_id => 908255,
:tpl_value => URI::encode('#code#=' + pin.to_s + '&#app#=' + name),
}
发送请求
res = Net::HTTP.post_form(uri, param)
学习笔记
require 'uri'
require 'net/http'
#========================
str_res = Net::HTTP.get('example.com', '/index.html')
#========================
uri = URI('http://www.baidu.com')
str_res = Net::HTTP.get(uri)
#========================
#get method
uri = URI('http://example.com/index.html')
params = {:limit => 10, :page => 3}
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
#========================
#post method
uri = URI('http://www.example.com/search.cgi')
res = Net::HTTP.post_form(uri, {'q' => 'ruby', 'max' => '50'})
puts res.body
#========================
1
uri = URI('http://example.com/some_path?query=string')
Net::HTTP.start(uri.host, uri.port) do |http|
request = Net::HTTP::Get.new uri
response = http.request request
end
2