我正试图通过代理从网址获取图片:
URL url = new URL("https://www.google.es/images/srpr/logo11w.png"); //Example
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy", 8080)); //Proxy address is http://proxy:8080
URLConnection conexión = url.openConnection(proxy);
conexión.connect();
Image imagen = ImageIO.read(url.openStream());
问题出在this.imagen = ImageIO.read(url.openStream());
:
java.net.ConnectException: Connection timed out: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at gui.Ventana.<init>(Ventana.java:34)
at servicio.Servicio2.buscar(Servicio2.java:60)
at data.Main.main(Main.java:20)
我还尝试使用Authenticator class
添加Authenticator.setDefault(new Autenticador(username, password));
public class Autenticador extends Authenticator {
private String usuario;
private String password;
public Autenticador(String usuario, String password) {
this.usuario = usuario;
this.password = password;
}
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(usuario, password.toCharArray());
}
}
}
我仍然得到同样的错误。有什么建议吗?感谢。