简体   繁体   中英

java use jvm proxy setting still returns 407 while postman works fine

Here is my postman config在此处输入图像描述

it works just find, can obtain my target url content, while in intellij, the code is like below,

  public static void main(String[] args) throws IOException, InterruptedException {
    System.setProperty("java.net.useSystemProxies", "true");
    String url = "http://xxxx/";

    String charset = "UTF-8";
    URLConnection connection = new URL(url).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);
    InputStream response = connection.getInputStream();
    String result = new BufferedReader(new InputStreamReader(response))
            .lines().collect(Collectors.joining("\n"));

    System.out.println(result);

}

I also have my jvm args added like

-Dhttp.proxyUser=username -Dhttp.proxyPassword=password -Dhttp.proxyHost=proxyhost -Dhttp.proxyPort=8080 

but above code keeps saying: Server returned HTTP response code: 407 for URL

Can someone tell me anything I missed here?

find answer from HTTP Error 407 Proxy authentication required and it worked, need to add below code

    Authenticator.setDefault(new Authenticator()
    {
        protected PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication("username","password".toCharArray());
        }
    });

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