问题
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:
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
来源:https://stackoverflow.com/questions/18348673/how-do-i-configure-a-ruby-mechanize-agent-to-work-through-the-charles-web-proxy