A simple example of sending a request based on the HTTP protocol in a ruby program

Time:2022-1-9

1. Establish HTTP connection (send request parameters through get)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
require "open-uri"
#If there is a get request, the parameters are written directly in the URI address
uri = 'http://uri'
html_response = nil
open(uri) do |http| 
html_response = http.read 
end
puts html_response 
require "open-uri"
#If there is a get request, the parameters are written directly in the URI address
uri = 'http://uri'
html_response = nil
open(uri) do |http|
html_response = http.read
end
puts html_response

2. Send request parameters through 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) 
#Returned cookie
puts res.header['set-cookie'
#Returned HTML body
puts res.body 
params = {}
params["name"] = 'Tom'
uri = URI.parse("http://uri")
res = Net::HTTP.post_form
(uri, params) 
#Returned cookie
puts res.header['set-cookie']
#Returned HTML body
puts res.body

3. HTTPS request

?
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
#
#Description:
#Send express data to the datasystem using HTTPS
#Input:
#Data - data of express after assembly
#Output:
#Status information returned by 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("Link address parameter: #{URI. Escape (URL)}, file name: #{_file _}, The #{#u line #} OK“)
   $logger.info("Passed in data parameter: #{data. To_json}, file name: #{_file_}, The #{#u line #} OK“)
  end
  request = Net::HTTP::Get.new(uri.request_uri)
   
  response = http.request(request)
 rescue =>exception
  $logger.error("The delivery URL address is #{URL}, error! #{exception. To_s}, file name: #{_file _}, The #{#u line #} OK“)
  return nil
 end
 return response.body
end

Recommended Today

Viewbinding uses view binding in , , adapter, fragment, and activity

Original addresshttps://blog.csdn.net/fly_with_24/article/details/104337067 The following is the author’s reading record original text Google’s role in 2019 I / O ConferenceWhat’s New in Architecture ComponentsThis paper introduces view binding InWhat’s New in Architecture ComponentsIn, there is a short speech on view binding, which compares view binding with existing solutions and further discusses why view binding is better […]