How do I configure a Ruby Mechanize agent to work through the Charles web proxy?

久未见 提交于 2019-12-07 06:13:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!