这样URLConnection connection timeout一个问题:

代码:

URL url= new URL(urlString);  
URLConnection conn=url.openConnection();

异常问题:

java.net.ConnectException: Connection timed out: 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:656)
	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:1104)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:998)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
	at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)

当上面的urlstring 为内部一个url链接时,没有问题。但是是公司外部链接时,出错了。于是想到了代理,公司局域网内没问题,访问外网通过代理。

解决办法:

1. 设置代理属性

System.setProperty("http.proxyHost", "proxy IP");
System.setProperty("http.proxyPort", "proxyPort");

注意如果你访问的是https类型的url. 请使用https.proxyHost 和 https.proxyPort。 当然还要设置其他SSL相关属性。

2. 设置代理

 String urlString="http://www.soosmart.com";       
          
        String proxyIp="proxyIP";          //代理Ip地址  
          
        int Port=80;   //代理提供的开放端口  
          
       try {  
        /*构造Proxy对象,以适用于代理上网的方式*/  
        InetSocketAddress socketAddress=new InetSocketAddress(  
                InetAddress.getByName(proxyIp),Port);  
          
        Proxy proxy=new Proxy(Proxy.Type.HTTP,socketAddress);  
             
           /*构造url对象*/  
        URL url= new URL(urlString);  
          
        /*测试是否能够打开连接,获得输入流,连接方式是直连方式*/  
//      InputStream inputStream=url.openConnection().getInputStream();  
          
        /*下面用代理的方式进行连接,需要构造Proxy对象*/  
        InputStream input=url.openConnection(proxy).getInputStream();  
          
        if(input !=null){  
            System.out.println("Connectioned");  
        }  
    } catch (MalformedURLException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    } catch (IOException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    } 

 

发表评论