Mechanize Proxy Configuration
Mechanize automates websites like a browser. Call set_proxy with host, port, and optional credentials parsed from your proxy URL, as in proxy-examples/ruby/mechanize-proxy.rb. Mechanize does not integrate with ruby-proxy-headers directly; for sticky IP or country headers over HTTPS, fetch with Faraday + ruby-proxy-headers or patched Net::HTTP, then parse pages with Mechanize if needed.
Installation
gem install mechanize
Basic Proxy Configuration
require 'mechanize'
require 'uri'
proxy = URI('http://username:password@proxyhost:31280')
agent = Mechanize.new
agent.set_proxy(proxy.host, proxy.port, proxy.user, proxy.password)
page = agent.get('https://api.ipify.org?format=json')
puts page.body
Custom Proxy Headers
Use Net::HTTP + RubyProxyHeaders::NetHTTP.patch! or Faraday + RubyProxyHeaders::FaradayIntegration when you need X-ProxyMesh-Country or similar on CONNECT. See ruby-proxy-headers.
ProxyMesh Headers Reference
Send these headers to control proxy behavior:
X-ProxyMesh-Country- Route through a specific country (e.g., "US"). Only works with world proxy or open proxyX-ProxyMesh-IP- Request a specific outgoing IP addressX-ProxyMesh-Not-IP- Exclude specific IPs from rotation
The proxy returns X-ProxyMesh-IP with the IP address used for the request (when your stack exposes CONNECT response headers).
Resources
- Mechanize Documentation
- Mechanize on RubyGems
- Mechanize on RubyDoc
- ruby-proxy-headers (Mechanize)
- ruby-proxy-headers on RubyGems
- ruby-proxy-headers on RubyDoc
- ProxyMesh Headers Reference
- Example Code on GitHub
Related Ruby Proxy Guides
Explore proxy configuration for other Ruby HTTP libraries: