How do I set the proxy to be used by the JVM

前端 未结 19 1924
孤街浪徒
孤街浪徒 2020-11-22 05:51

Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.

I am behind

19条回答
  •  余生分开走
    2020-11-22 06:16

    I am also behind firewall, this worked for me!!

    System.setProperty("http.proxyHost", "proxy host addr");
    System.setProperty("http.proxyPort", "808");
    Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
    
            return new PasswordAuthentication("domain\\user","password".toCharArray());
        }
    });
    
    URL url = new URL("http://www.google.com/");
    URLConnection con = url.openConnection();
    
    BufferedReader in = new BufferedReader(new InputStreamReader(
                        con.getInputStream()));
    
    // Read it ...
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    
    in.close();
    

提交回复
热议问题