6

I'm writing an "automatically fill in the forms" app using Ruby / Mechanize. It almost works.

I can use the wonderful Charles web proxy to see the exchange between the server and my Firefox browser. Now I want to use Charles to see the exchange between the server and my app.

Charles proxies on port 8888. Assume that the server is at https://my.host.com. One thing that does NOT work is:

@agent ||= Mechanize.new do |agent|
  agent.set_proxy("my.host.com", 8888)
end

This results in a Net::HTTP::Persistent::Error:

...lib/net/http/persistent.rb:579:in `rescue in connection_for': connection refused: my.host.com:8888 (Net::HTTP::Persistent::Error)

So either I'm giving the wrong host argument to agent.set_proxy(host, ...), or I haven't configured Charles properly. (FWIW, I used to be able to do this, but both Mechanize and Charles have matured several generations since those halcyon days...)

Any ideas?

1 Answer 1

11

A web proxy is not normally defined by just a port, but is usually a full host name. Charles is very likely installed on localhost. Therefore the following adjustment may work for you:

@agent ||= Mechanize.new do |agent|
  agent.set_proxy("localhost", 8888)
end
Sign up to request clarification or add additional context in comments.

3 Comments

For the record the tap is unnecessary, Mechanize.new yields itself to the block.
Thanks -- I should have realized that localhost was the right thing. It almost works -- now all I have to do is get my certificate verify failed errors taken care of. Different problem...
@pguardiario: thanks for the tip. edited the OP (and my source code) accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.