简体   繁体   中英

Getting connection timeout to URL, which I'm otherwise able to connect via browser in the same system

Due to company policies, I cannot share the complete code here, but here is where the code fails:

try(InputStream in = new URL("URL with image file").openStream()){
            Files.copy(in, Paths.get("file://newfile.png"));
        } catch (Exception e) {
    }

The Exception is:

java.net.ConnectException: Connection timed out: connect

The things I have tried:

  1. Adding proxy and port number before openstream method
  2. Adding User agent
  3. Using system proxy settings

Note: I'm trying to save an image from the URL. I have tried accessing the URL with my personal system and it's working. With my company system, it's not, even after adding the proxy.

Could someone please tell me why this is happening?

Sorry, it was my mistake. If anyone is seeing this:

The problem came from how I used proxy. If you are NOT able to connect to a website which is otherwise accessible via browser in same system , then you need a proxy in your java code.

For example:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("google.com", 80));

Hope this helps. If you have any doubts regarding this, I'll be happy to help.

Note : Don't use "https" for proxy. Give only domain name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM