我是Java的初学者,我的第一个任务是解析一些10,000个网址并从中提取一些信息,为此我使用了Jsoup,并且它运行良好。
但现在我想添加代理支持。这些代理还有用户名和密码。
你可以轻松设置代理
System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
Document doc = Jsoup.connect("www.google.com").get();
Jsoup.proxy(proxy)
。 - jpllosaJsoup 1.9.1 及以上版本:(推荐方法)
// Fetch url with proxy
Document doc = Jsoup //
.connect("http://www.example.com/") //
.proxy("127.0.0.1", 8080) // sets a HTTP proxy
.userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2") //
.header("Content-Language", "en-US") //
.get();
您还可以使用重载方法 Jsoup#proxy,该方法接受一个Proxy类(见下文)。
在 Jsoup 1.9.1 之前的版本中: (冗长的方式)// Setup proxy
Proxy proxy = new Proxy( //
Proxy.Type.HTTP, //
InetSocketAddress.createUnresolved("127.0.0.1", 8080) //
);
// Fetch url with proxy
Document doc = Jsoup //
.connect("http://www.example.com/") //
.proxy(proxy) //
.userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2") //
.header("Content-Language", "en-US") //
.get();
参考资料:
java.net.ConnectException: Connection refused: connect
。 - Nakul Sharmajava.net.ConnectException: Connection refused: connect
可能表示目标主机未监听或拒绝了某些客户端连接。 - Stephan您不必通过Jsoup获取网页数据。这是我的解决方案,尽管可能不是最好的。
URL url = new URL("http://www.example.com/");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); // or whatever your proxy is
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
String line = null;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((line = in.readLine()) != null) {
tmp.append(line);
}
Document doc = Jsoup.parse(String.valueOf(tmp));
就是这样。通过代理获取HTML页面的源代码,然后使用Jsoup解析。
final String authUser = "USERNAME";
final String authPassword = "PASSWORD";
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
..
System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
..
System.setProperty("http.proxyHost", "192.168.5.1");
System.setProperty("http.proxyPort", "1080");
Document doc = Jsoup.connect("www.google.com").get();
这个解决方案是错误的,因为解析通常是多线程的,我们通常需要更改代理。这段代码为所有线程只设置了一个代理。所以最好不要使用Jsoup.Connection。
Connection
类具有以下方法:
proxy(Proxy p)
proxy(String host, int port)
您可以像这样使用它:
Jsoup.connect("...url...").proxy("127.0.0.1", 8080);
Authenticator
方法,如 @Navneet Swaminathan 所提到的,或者简单地设置系统属性:System.setProperty("http.proxyUser", "username");
System.setProperty("http.proxyPassword", "password");
或者
System.setProperty("https.proxyUser", "username");
System.setProperty("https.proxyPassword", "password");
URL url = new URL("http://www.example.com/");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); // or whatever your proxy is
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
hc.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.4; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2");
uc.setRequestProperty("Content-Language", "en-US");
uc.setRequestMethod("GET");
uc.connect();
Document doc = Jsoup.parse(uc.getInputStream());