End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
Net::HTTP.new
# File tmp/rubies/ruby-3.2.0/lib/net/http.rb, line 802
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil)
http = super address, port
if proxy_class? then # from Net::HTTP::Proxy()
http.proxy_from_env = @proxy_from_env
http.proxy_address = @proxy_address
http.proxy_port = @proxy_port
http.proxy_user = @proxy_user
http.proxy_pass = @proxy_pass
elsif p_addr == :ENV then
http.proxy_from_env = true
else
if p_addr && p_no_proxy && !URI::Generic.use_proxy?(p_addr, p_addr, p_port, p_no_proxy)
p_addr = nil
p_port = nil
end
http.proxy_address = p_addr
http.proxy_port = p_port || default_port
http.proxy_user = p_user
http.proxy_pass = p_pass
end
http
end
Returns a new Net::HTTP object http (but does not open a TCP connection or HTTP session).
No Proxy
With only string argument hostname given (and ENV['http_proxy'] undefined or nil), the returned http:
-
Has the given address.
-
Has the default port number,
Net::HTTP.default_port(80). -
Has no proxy.
Example:
http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.address # => "jsonplaceholder.typicode.com" http.port # => 80 http.proxy? # => false
With integer argument port also given, the returned http has the given port:
http = Net::HTTP.new(hostname, 8000) # => #<Net::HTTP jsonplaceholder.typicode.com:8000 open=false> http.port # => 8000
Proxy Using Argument p_addr as a String
When argument p_addr is a string hostname, the returned http has a proxy:
http = Net::HTTP.new(hostname, nil, 'proxy.example') # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.proxy? # => true http.proxy_address # => "proxy.example" # These use default values. http.proxy_port # => 80 http.proxy_user # => nil http.proxy_pass # => nil
The port, username, and password for the proxy may also be given:
http = Net::HTTP.new(hostname, nil, 'proxy.example', 8000, 'pname', 'ppass') # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.proxy? # => true http.proxy_address # => "proxy.example" http.proxy_port # => 8000 http.proxy_user # => "pname" http.proxy_pass # => "ppass"
Proxy Using ENV['http_proxy']
When environment variable 'http_proxy' is set to a URI string, the returned http will have that URI as its proxy; note that the URI string must have a protocol such as 'http' or 'https':
ENV['http_proxy'] = 'http://example.com' # => "http://example.com" http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.proxy? # => true http.address # => "jsonplaceholder.typicode.com" http.proxy_address # => "example.com"
The URI string may include proxy username, password, and port number:
ENV['http_proxy'] = 'http://pname:[email protected]:8000' # => "http://pname:[email protected]:8000" http = Net::HTTP.new(hostname) # => #<Net::HTTP jsonplaceholder.typicode.com:80 open=false> http.proxy_port # => 8000 http.proxy_user # => "pname" http.proxy_pass # => "ppass"
Argument p_no_proxy
You can use argument p_no_proxy to reject certain proxies:
-
Reject a certain address:
Examplehttp = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example') http.proxy_address # => nil
-
Reject certain domains or subdomains:
Examplehttp = Net::HTTP.new('example.com', nil, 'my.proxy.example', 8000, 'pname', 'ppass', 'proxy.example') http.proxy_address # => nil
-
Reject certain addresses and port combinations:
Examplehttp = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:1234') http.proxy_address # => "proxy.example" http = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'proxy.example:8000') http.proxy_address # => nil
-
Reject a list of the types above delimited using a comma:
Examplehttp = Net::HTTP.new('example.com', nil, 'proxy.example', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000') http.proxy_address # => nil http = Net::HTTP.new('example.com', nil, 'my.proxy', 8000, 'pname', 'ppass', 'my.proxy,proxy.example:8000') http.proxy_address # => nil