How to make JSoup access the web via Tor

When Tor is started in default configuration, it opens SOCKS port 9050.
(With SocksPort in torrc file Tor can be configured to use different port)
Which means that you can start Tor and run this program:

package com.example.jsoup_tor;
import java.net.Proxy;
import java.net.InetSocketAddress;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

public class JsoupTor {
    public static void main(String[] args) {
        try {
            Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 9050));
            Document doc = Jsoup.connect("https://2ip.ru").proxy(proxy).get();
            String ip = doc.select("div.ip > span").first().html();
            System.out.printf("Your IP is: %s\n", ip);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(I’m not Java programmer, so this code may be not perfect :slight_smile: )