如何将 Tor 与 Java 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29653758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:40:25  来源:igfitidea点击:

How to use Tor combine with Java

javaautorotatetor

提问by Leo Le

Updated my question

更新了我的问题

I'm building a crawler system by Java to compare price online. However, I worry about my IP address can be banned. So I intend to use proxy to change IP dynamic or use some tools to rotate IP automatically.

我正在通过 Java 构建一个爬虫系统来在线比较价格。但是,我担心我的 IP 地址会被禁止。所以我打算使用proxy来动态改变IP或者使用一些工具来自动轮换IP。

Many people said that TOR is a powerful tool to rotate IP. However, I don't know how to use Tor and how to integrate Tor to Java Web Application ?

很多人说TOR是一个强大的IP轮换工具。但是,我不知道如何使用 Tor 以及如何将 Tor 集成到 Java Web 应用程序中?

I've search Google to find example but still find nothing useful.

我已经在谷歌上搜索过例子,但仍然没有找到任何有用的东西。

Anyone can help me.

任何人都可以帮助我。

采纳答案by Chris Dennett

You'll just need to get Java to use the SOCKS4 proxy at localhost:8118(8118 is the default Tor port) when it makes an outgoing HTTP connection that uses a URL (use URLConnection), while the Tor service is running. See herefor how to use proxies in Java 8.

您只需要让 Java 使用 SOCKS4 代理localhost:8118(8118 是默认 Tor 端口),当它建立使用 URL(使用URLConnection)的传出 HTTP 连接时,同时 Tor 服务正在运行。请参阅此处了解如何在 Java 8 中使用代理。

Edit: there is also this pure Java Tor librarythat you may be able to use, either directly or through minor modification (if it acts entirely like the normal native Tor service), but it hasn't been updated in a while so may not be compatible with the latest Tor specification.

编辑:还有这个纯 Java Tor 库,您可以直接或通过小修改使用(如果它完全像普通的本地 Tor 服务一样),但它已经有一段时间没有更新了,所以可能不会与最新的 Tor 规范兼容。

HttpClient example:

HttpClient 示例:

HttpHost proxy = new HttpHost("127.0.0.1", 8118, "http");

DefaultHttpClient httpclient = new DefaultHttpClient();
try {
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

    HttpHost target = new HttpHost("www.google.com", 80, "http");
    HttpGet req = new HttpGet("/");

    System.out.println("executing request to " + target + " via " + proxy);
    HttpResponse rsp = httpclient.execute(target, req);
    ...
} finally {
    // When HttpClient instance is no longer needed,
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();
}

Note that you musthave the Tor service running for this.

请注意,您必须为此运行 Tor 服务。