I want to check if a bunch of URLs are working. So, I wrote some code (given below) to do that. It works for sites like google.com. When I apply it to my scenario, it fails.
I am logging into a VM. From this VM, I can open the desired URL in a browser. When I try to check if I can connect to the URL with code, it fails. The URL obtained by my code from file is correct and works on browser. So, an error in the URL is ruled out.
My server urls look like this -
ab-web-internal-test-005.myweb.com
How do I debug this issue and enable my code to connect to the URL?
This is the exception:
C:/mycode/>ruby LinkTester.rb
C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `initialize': No connection could be made because the target machine actively refused it. -
connect(2) (Errno::ECONNREFUSED)
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `open'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:878:in `block in connect'
from C:/Ruby200-x64/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:877:in `connect'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:851:in `start'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:582:in `start'
from C:/Ruby200-x64/lib/ruby/2.0.0/net/http.rb:477:in `get_response'
from LinkTester.rb:9:in `connect_to_url'
from LinkTester.rb:38:in `block in <main>'
from LinkTester.rb:37:in `each'
from LinkTester.rb:37:in `<main>'
Code -
require "net/http"
require "uri"
def connect_to_url(url)
response = nil
encoded_uri = URI.encode(url)
uri = URI.parse(encoded_uri)
response = Net::HTTP.get_response(uri)
http = Net::HTTP.new(uri.host, uri.port)
response = http.request(Net::HTTP::Get.new(uri.request_uri))
case http_response
when Net::HTTPSuccess
puts uri + "success"
when Net::HTTPRedirect
puts uri + "success"
else
puts uri + "failure"
end
end
def get_urls(file_path)
array = Array.new
file = File.open(file_path, "r")
file.each_line do |line|
array << line
end
file.close
return array
end
url_file = "C:/mycode/servers.txt"
url_array = get_urls(url_file);
url_array.each do |url|
connect_to_url(url)
end