My HTTP code is running against either proxy or no-proxy. Something like:
@uri = URI(testrail_url)
# With Proxy
# proxy_url = "http://URL:port"
# proxy_uri = URI(proxy_url)
# proxy = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port)
# @http = proxy.new(@uri.host, 443)
# Without Proxy
@http = Net::HTTP.new(@uri.host, 443)
Please suggest a way to handle both the code in the same block based on the network is on proxy or no-proxy.
Below my suggestion. Ideally, put it in a separated module.
require 'net/http'
proxy = {
host: 'proxy host',
port: 8080,
user: 'login',
password: 'password'
}
uri_str = "http://google.com"
begin
uri = URI.parse(uri_str)
http = if proxy
::Net::HTTP::Proxy(
proxy[:host],
proxy[:port],
proxy[:user],
proxy[:password]
).new(uri.host, uri.port)
else
::Net::HTTP.new(uri.host, uri.port)
end
response = http.get(uri.request_uri)
rescue Exception => e
puts "There was an error: #{uri_str} #{e.message}"
puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.