title: How to send a request to localhost using 'net/http' fails with end of file reached (EOFError)
body: <p>I'm using Ruby version 2.3.0.</p>
  
  <p>I want to check when my application is up, and I wrote this method for my "deployer".
  At runtime <code>http.request_get(uri)</code> raises</p>
  
  <pre><code>EOFError: end of file reached
  </code></pre>
  
  <p>when I pass <code>http://localhost</code> as a first argument into the method:</p>
  
  <pre><code>require 'net/http'
  
  def check_start_application(address, port)
    success_codes = [200, 301]
    attempts = 200
    uri = URI.parse("#{address}:#{port}")
    http = Net::HTTP.new(uri.host, uri.port)
  
    attempts.times do |attempt|
      # it raises EOFError: end of file reached
      http.request_get(uri) do |response|
        if success_codes.include?(response.code.to_i)
          return true
        elsif attempt == attempts - 1
          return false
        end
      end
    end
  end
  </code></pre>
  
  <p>But, when I test this method separately from a context with irb, this code works pretty well for two cases:</p>
  
  <pre><code>check_start_application('http://example.com', '80')
  check_start_application('http://localhost', any_port)
  </code></pre>
  
  <p>In an app's context this code works for only one case:</p>
  
  <pre><code>check_start_application('http://example.com', '80')
  </code></pre>
  
  <p>What I tried:</p>
  
  <ul>
  <li>using 'rest-client' instead of 'net/http'</li>
  <li>using 'net/https' with <code>http.use_ssl = false</code></li>
  <li>remove <code>times</code> from the method </li>
  <li>call <code>sleep</code> before a request</li>
  </ul>
  
  <p>Who faced a similar problem? I believe I'm not the only one.</p>