我正在尝试使用Jsoup解析一些url但我收到此错误:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
这是我的代码:
public Elements getLinks(String link){
Document doc = null;
Elements links = null;
try {
doc = Jsoup.connect(link)
.userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.referrer("http://www.google.com")
.timeout(5000) //it's in milliseconds, so this means 5 seconds.
.get();
links = doc.select("a[href]");
}
catch (ConnectException e){
e.printStackTrace();
}
catch (UnsupportedMimeTypeException e){
e.printStackTrace();
}
catch (SocketTimeoutException e){
e.printStackTrace();
}
catch (SSLException e) {
e.printStackTrace();
}
catch (IOException e) {
if(e.getCause() instanceof SocketTimeoutException) {
try {
throw new SocketTimeoutException();
} catch (SocketTimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
e.printStackTrace();
}
return links;
}
我有一个关于ConnectException的catch部分,但我的程序停止..有任何想法如何解决这个问题?