我正在尝试通过提供URL连接到服务器。但是,我收到以下错误
11:48:41.936 [qtp62610667-23] INFO pl.imguploadimg - java.net.ConnectException: Connection refused: connect
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
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.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:173)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
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.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1038)
at pl.imguploadimg.webapp.service.HTMLService.findImagesInInputStream(HTMLService.java:36)
at pl.imguploadimg.webapp.controller.BaseController.socketConnection(BaseController.java:49)
切换到LAN时发生此问题。以下是我的代码:
@Value("${useSystemProxy}")
Boolean useSystemProxy;
public void findImagesInInputStream(String url) throws IOException {
if (useSystemProxy) {
System.setProperty("java.net.useSystemProxies", "true");
}
URL postURL = new URL(url);
InputStream inputStream = postURL.openStream();
findMIME(inputStream);
}
此处String url = "https://www.google.co.in/webhp?hl=en";
此外,同一程序在主函数
中运行public static void main(String args[]) throws IOException {
System.setProperty("java.net.useSystemProxies", "true");
URL myURL = new URL("http://www.macdowellcolony.org/WorkSampleRequirements.pdf");
URLConnection myURLConnection = myURL.openConnection();
InputStream isr = myURL.openStream();
myURLConnection.connect();
System.out.println(myURLConnection.getHeaderFields());
System.out.println(myURLConnection.getContentType());
}
提前致谢。
答案 0 :(得分:1)
以下代码更改得以解决:
@Value("${useSystemProxy}")
Boolean useSystemProxy;
@Value("${proxyURL}")
String proxyURL;
@Value("${proxyPort}")
String proxyPort;
@Value("${username}")
String username;
@Value("${password}")
String password;
public void findImagesInInputStream(String url) throws IOException {
if (useSystemProxy) {
System.setProperty("java.net.useSystemProxies", "true");
System.getProperties().put("http.proxyHost", proxyURL);
System.getProperties().put("http.proxyPort", proxyPort);
System.getProperties().put("http.proxyUser", username);
System.getProperties().put("http.proxyPassword", password);
}
URL postURL = new URL(url);
InputStream inputStream = postURL.openStream();
findMIME(inputStream);
}
答案 1 :(得分:0)
也许问题出在服务器端。当服务器检测到不允许客户端ip时,它只是拒绝连接(就像stacktrace所说的那样)。