1. Establish an HTTP connection (send request parameters by Get method)
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. Send request Parameters via post
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["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 Request
#
Description:
# send courier data to Datasystem, use HTTPS
# input: # data -assembled Expess's
output: # Datasystem return status Information
#
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 ("link address parameter: #{uri.escape (URL)}, FileName: #{__file__}, #{__line__} line")
$ Logger.info ("Incoming data parameter: #{data.to_json}, FileName: #{__file__}, #{__line__}") End
request = Net::http::get.new (Uri.request_uri)
Response = http.request (Request)
rescue =>exception
$logger. Error ("Delivery URL address is #{url}, error!#{exception.to_ s}, FileName: #{__file__}, #{__line__} line ") Return
Nil end
return
Response.body