I'm trying to read a website through Tor in a java app with the silvertunnel-ng library. The connection seems to get build (log says "To start completed!"), but when I try to read the site I get a
java.net.ProtocolException: Server redirected too many times (3)
The test code is this:
String torCheck = "https://check.torproject.org/?lang=en_US";
URLStreamHandler handler = TorTools.buildTorHandler();
String result = "";
try {
URL url = new URL(null, torCheck, handler);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
System.setProperty("http.maxRedirects", "20");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while (br.read() != -1)
result += br.readLine() + "\n";
} catch (IOException e) {
...
} finally {
...
}
System.out.println(result);
Apparently the httpRedirects property doesn't get set since the stacktrace still says "(3)", but I have no idea why.
Adding the CookieHandler was suggested here, but to no avail.
The Tor handler is build with this method:
import org.silvertunnel_ng.netlib.adapter.url.NetlibURLStreamHandlerFactory;
import org.silvertunnel_ng.netlib.api.NetFactory;
import org.silvertunnel_ng.netlib.api.NetLayer;
import org.silvertunnel_ng.netlib.api.NetLayerIDs;
public static URLStreamHandler buildTorHandler() {
NetLayer netLayer = NetFactory.getInstance().getNetLayerById(NetLayerIDs.TOR);
netLayer.waitUntilReady();
NetlibURLStreamHandlerFactory factory = new NetlibURLStreamHandlerFactory(false);
factory.setNetLayerForHttpHttpsFtp(netLayer);
return factory.createURLStreamHandler("http");
}
Any idea would be much appreciated.