问题描述
更新了我的问题
我正在构建一个Java爬虫系统,以便在线比较价格。但是,我担心我的IP地址可以被禁止。因此我打算使用代理来更改IP动态或使用一些工具自动轮换IP。
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.
很多人都说TOR是一个强大的旋转IP工具。但是,我不知道如何使用Tor以及如何将Tor集成到Java Web应用程序中?
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 ?
我搜索Google以找到示例,但仍然没有找到任何有用的东西。
I've search Google to find example but still find nothing useful.
任何人都可以帮助我。
推荐答案
你只需要让Java在 localhost:8118
(8118是默认的Tor端口)时使用SOCKS4代理,当它建立使用URL的传出HTTP连接时(使用<$ c $当Tor服务正在运行时,c> URLConnection )。有关如何在Java 8中使用代理,请参见。
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 here for how to use proxies in Java 8.
编辑:你还有可以直接使用,也可以通过微小的修改(如果它完全像普通的本机Tor服务那样),但它一段时间没有更新,所以可能与最新的Tor规范不兼容。
there is also this pure Java Tor library that 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.
HttpClient示例:
HttpClient example:
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();
}
请注意必须运行Tor服务为此。
Note that you must have the Tor service running for this.
这篇关于如何使用Tor与Java结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!