Ruby Net::HTTP Proxy Setup Guide

To use RotatingProxy with Ruby's Net::HTTP library, you set the proxy host, port, username, and password when creating the HTTP object. This guide covers the exact syntax for Net::HTTP, Faraday, and other common HTTP clients in the Ruby ecosystem.

Key points
  • Use Net::HTTP.new with the :proxy_host, :proxy_port, :proxy_user, and :proxy_pass keyword arguments.
  • Faraday supports proxy configuration via the Faraday.new connection options.
  • Target locations and manage sessions by appending options like _cc.us or _sid.abc123 to the proxy username.
  • The SOCKS5 port is 7071, usable with libraries that support SOCKS proxies like HTTPX.

Ruby Net::HTTP Proxy Configuration

The standard Net::HTTP library has built-in proxy support. You configure it by passing proxy parameters when you create the Net::HTTP object. The gateway host is gw.rotatingproxy.cc, and the primary port for HTTP and HTTPS traffic is 7070. Here is the basic pattern using keyword arguments, which is the recommended modern approach:

require 'net/http'
require 'uri'

uri = URI('https://httpbin.org/ip')
proxy_user = 'USERNAME_net.res_cc.us' # Example with targeting
proxy_pass = 'PASSWORD'

Net::HTTP.start(uri.host, uri.port,
  proxy_host: 'gw.rotatingproxy.cc',
  proxy_port: 7070,
  proxy_user: proxy_user,
  proxy_pass: proxy_pass,
  use_ssl: uri.scheme == 'https'
) do |http|
  request = Net::HTTP::Get.new(uri)
  response = http.request(request)
  puts response.body
end

This method opens a connection through the proxy and handles the CONNECT tunnel for HTTPS automatically. The proxy_user string is where you embed your targeting and session options, as detailed later. For tasks like web scraping or price monitoring, you'll typically wrap this logic in a method that can handle rotation and errors.

Using Faraday with RotatingProxy

Faraday is a popular HTTP client library that offers more flexibility and middleware. To configure Faraday to use the proxy, set the :proxy option in the connection initialization. This is ideal for building more complex clients for rank tracking or AI training data collection.

require 'faraday'

proxy_user = 'USERNAME_net.dc_cc.de'
proxy_pass = 'PASSWORD'

conn = Faraday.new(
  url: 'https://httpbin.org',
  proxy: {
    uri: 'http://gw.rotatingproxy.cc:7070',
    user: proxy_user,
    password: proxy_pass
  }
)

response = conn.get('/ip')
puts response.body

Faraday will use this proxy configuration for all requests made through this connection object. You can also set the proxy per request, but setting it at the connection level is more efficient. Check the current Faraday docs if you are using a version older than 1.0, as the proxy option syntax was slightly different.

SOCKS5 Proxy Setup in Ruby

RotatingProxy's SOCKS5 gateway runs on port 7071. Standard Net::HTTP and Faraday do not natively support SOCKS5. To use it, you need a library that adds SOCKS support, such as http (the HTTPX gem) or socksify. The HTTPX gem is a modern, concurrent HTTP client with built-in SOCKS5 support. This can be useful for certain network configurations or tools.

require 'httpx'

proxy_url = 'socks5://USERNAME_net.mob_cc.jp:PASSWORD@gw.rotatingproxy.cc:7071'

HTTPX.with_proxy(proxy_url).get('https://api.ipify.org') do |response|
  puts response.body
end

If your project relies on Net::HTTP, you can use the socksify gem to patch the socket layer, but this approach is global and may affect other parts of your application. For most use cases, especially social media management or Instagram proxies, the HTTP port 7070 is sufficient and more widely compatible.

Building the Proxy Username with Options

Authentication and proxy behavior are controlled through the username string. Your base login is appended with underscore-separated options. The password remains your proxy user's plain password. This system allows you to specify the network, geographic target, and session persistence without changing your code.

  • Network: Choose _net.res (residential), _net.mob (mobile), _net.dc (datacenter), or _net.isp (ISP). Residential is the default if omitted.
  • Country: Use _cc.us for the United States, _cc.de for Germany, etc. The code must be lowercase ISO 3166-1 alpha-2.
  • US State: Only works with cc.us. Example: _st.ca for California.
  • City: Use tokens like _city.chicago or _city.new-york. Check the dashboard for the exact token list.
  • Sticky Session: Add _sid.abc123 (6-16 chars) and optionally _ttl.30 (1 to 120 minutes, default 10).

Example username for a mobile proxy in Berlin with a 20-minute session: USERNAME_net.mob_cc.de_city.berlin_sid.a7k2m9_ttl.20. This is crucial for tasks that require consistent IPs, like managing ad verification or Facebook proxies.

Verifying the Exit IP and Location

After setting up your proxy, always verify that traffic is exiting from the expected IP and location. A simple GET request to a service that returns your IP address confirms the proxy is working. This is the first step in testing a proxy connection.

require 'net/http'

def get_ip(proxy_user, proxy_pass)
  uri = URI('https://api.ipify.org')
  Net::HTTP.start(uri.host, uri.port,
    proxy_host: 'gw.rotatingproxy.cc',
    proxy_port: 7070,
    proxy_user: proxy_user,
    proxy_pass: proxy_pass,
    use_ssl: true
  ) do |http|
    return http.get(uri).body
  end
end

ip = get_ip('USERNAME_net.res_cc.ca', 'PASSWORD')
puts "Exit IP is: #{ip}"
# For more details like location, use ipinfo.io/json

For more detailed geolocation data, you can use https://ipinfo.io/json. This returns a JSON object with country, region, city, and ISP information. Matching this output to your targeting options confirms correct setup for geo-content testing.

Common Errors and Fixes

When requests fail, the error message and HTTP status code point to the issue.

407 Proxy Authentication Required

Check that the username and password are exactly as shown in your dashboard. Ensure the username string is correctly formatted with underscores, not other punctuation. Double-check for typos. For a dedicated guide, see error 407.

Connection Timeouts

Timeouts can occur if the target server is slow, the proxy network is congested, or your own connection is unstable. Increase the timeouts in your HTTP client. In Net::HTTP, set open_timeout and read_timeout.

Net::HTTP.start(uri.host, uri.port,
  proxy_host: 'gw.rotatingproxy.cc',
  proxy_port: 7070,
  proxy_user: proxy_user,
  proxy_pass: proxy_pass,
  use_ssl: true,
  open_timeout: 30, # seconds to open TCP connection
  read_timeout: 60  # seconds to wait for data
) do |http|
  # ...
end

Also review proxy timeouts for more context.

TLS/SSL Errors

Errors like "certificate verify failed" may happen if your Ruby environment's CA certificates are outdated. This is not typically a proxy issue. Ensure your system's certificates are up to date (update-ca-certificates on Linux, or update Ruby via your package manager). You can temporarily bypass verification for testing with verify_mode: OpenSSL::SSL::VERIFY_NONE, but do not use this in production.

Tips for Running at Scale

When making many concurrent requests, you need to manage connections, sessions, and costs efficiently. This applies to large-scale market research or Google search proxies operations.

  • Distribute your workload across multiple threads or processes, but stay under this limit. Consider using a thread pool or an async library like async-http.
  • Session Management: For tasks that require a consistent IP (like adding items to a cart), use a unique sid (session ID). For rotation (like scraping search results), omit the sid to get a new IP per connection.
  • Traffic Efficiency: Traffic is metered as upload plus download. Enable gzip compression in your requests (Accept-Encoding: gzip), cache static resources locally, and avoid downloading large files like images unless necessary.
  • Error Handling and Retries: Implement a solid retry logic with exponential backoff. Transient network errors or a target site returning a 429/503 are common. Rotate sessions or introduce delays between retries.

Integrating with Other Ruby HTTP Clients

Beyond Net::HTTP and Faraday, you might use other gems. The configuration principle remains the same: provide the proxy host, port, and credentials.

HTTP.rb / HTTPX

The http gem (also known as HTTPX in newer versions) uses a different syntax. It supports both HTTP and SOCKS5 proxies via the :proxy option.

require 'http'

proxy_user = 'USERNAME_net.isp_cc.gb'
proxy_pass = 'PASSWORD'

client = HTTP.via('gw.rotatingproxy.cc', 7070, proxy_user, proxy_pass)
response = client.get('https://httpbin.org/headers')
puts response

RestClient

RestClient allows proxy configuration via environment variables or directly in the request.

require 'rest-client'

proxy_str = 'http://USERNAME_net.res:PASSWORD@gw.rotatingproxy.cc:7070'
response = RestClient::Request.execute(
  method: :get,
  url: 'https://httpbin.org/ip',
  proxy: proxy_str
)
puts response.body

Remember to embed targeting options in the username within the proxy string.

Monitoring Usage and Managing Costs

With a pay-as-you-go model, it's important to track your traffic. Your dashboard shows usage broken down by day and network. Use the traffic measurement guide to understand what counts toward your bill. The price per GB automatically steps down as your monthly volume increases on a network (except mobile, which has a flat rate). For example, your first 50 GB of residential traffic in a month cost $1.79 per GB, but after 1,000 GB, the rate drops to $1.20 per GB. This makes high-volume projects like news monitoring more economical. Always set monthly GB caps on your proxy users in the dashboard to prevent unexpected usage, especially when sharing credentials with team members or automated systems.

Questions

Can I integrate RotatingProxy with Ruby on Rails apps?

Yes, you can configure the proxy for outgoing HTTP requests made by your Rails app. For HTTP clients like Net::HTTP or Faraday used within controllers or jobs, follow the same setup patterns. Be mindful of connection pooling and timeouts in a production environment.

How do I target a specific city like Los Angeles?

First, check the city list in your dashboard or via the API to get the exact token (e.g., 'los-angeles'). Then, append `_city.los-angeles` to your proxy username, along with the country code (e.g., `_cc.us`). The full username might look like `USERNAME_net.res_cc.us_city.los-angeles`.

My script works without a proxy but fails with a 403 when using the proxy. Why?

The target site may be blocking the proxy IP due to its reputation or because it detects proxy traffic. This is common with sites like Amazon or LinkedIn. Try switching to a different network (e.g., from datacenter to residential), add realistic headers (User-Agent, Accept-Language), and implement respectful delays between requests. See our guide on avoiding blocks.

Does the Net::HTTP library in Ruby have built-in SOCKS5 proxy support?

No, the standard Net::HTTP library only supports HTTP proxy tunneling (CONNECT method). To use the SOCKS5 port (7071), you need an additional library like `socksify` that intercepts socket creation, or use a different HTTP client like HTTPX that has built-in SOCKS support.

How can I generate a unique session ID for each user in my application?

Create a random string of 6 to 16 lowercase letters and digits for each user or task. In Ruby, you can use `SecureRandom.alphanumeric(10).downcase`. Append it as `_sid.yourstring` to the proxy username. This ensures each user gets a consistent IP for the session TTL.

I'm getting a "connection refused" error. What should I check?

Verify the gateway host (`gw.rotatingproxy.cc`) and port (`7070` or `7071`) are correct. Make sure your firewall permits outbound TCP traffic to these ports. Also, confirm your account balance is not zero, as traffic stops when the balance is depleted. You can top up via the dashboard.

Start with one top-up, use any network

Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.