2015-11-16 150 views
1

我試圖通過提供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()); 
} 

提前感謝同一程序運行。

回答

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); 
} 
0

也許問題出在服務器端。當服務器檢測到客戶端IP不被允許時,它只是拒絕連接(就像stacktrace說的那樣)。

+0

我已經更新了說明。 –

相關問題