我对如何解决此问题或更新设置感到茫然。这是我得到的错误:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:649)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:449)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:434)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:181)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:170)
at final_project.GetHyperlinks.main(GetHyperlinks.java:17)
这是我试图运行的代码:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class GetHyperlinks {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack" , "true");
System.setProperty("javax.net.ssl.trustStore","C:/Program Files (x86)/Java/jdk1.8.0_25/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword","changeit");
String url = "https://stackoverflow.com/questions/2835505";
Document document = Jsoup.connect(url).get();
String question = document.select("#question .post-text").text();
System.out.println("Question: " + question);
Elements answerers = document.select("#answers .user-details a");
for (Element answerer : answerers) {
System.out.println("Answerer: " + answerer.text());
}
}
}
我已更新密钥库,设置_JAVA_OPTIONS env变量。我的HTTP_PROXY env变量是正确的。我需要在eclipse或jvm中设置代理信息吗?
答案 0 :(得分:1)
我使用了https.properties并成功连接:
public static void jsoupSimple()
{
try
{
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("java.version"));
System.setProperty(
"javax.net.ssl.trustStore",
System.getProperty("java.home") + "/lib/security/cacerts"
);
System.setProperty("https.proxyHost", "host");
System.setProperty("https.proxyPort", "port");
String url = "https://ru.stackoverflow.com/feeds/tag?tagnames=java&sort=newest";
Map<String, String> headers = new HashMap();
headers.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0");
Document doc = Jsoup.connect(url).headers(headers).get();
String html = doc.html();
System.out.println(html);
} catch (IOException ex)
{
Logger.getLogger(JsoupTest.class.getName()).log(Level.SEVERE, null, ex);
}
}