IK.AM

@making's tech note


Javaプログラム内でHTTPプロキシの認証を突破するための設定

🗃 {Programming/Java/java/net/Authenticator}
🗓 Updated at 2013-10-28T00:29:13Z  🗓 Created at 2013-10-28T00:29:13Z   🌎 English Page

いつも忘れるのでメモ

package sample;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

import org.springframework.web.client.RestTemplate;

public class Main {

    static {
        System.setProperty("http.proxyHost", "proxy host");
        System.setProperty("http.proxyPort", "port");
        Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("domain\\username", "password"
                        .toCharArray());
            }
        });
    }

    public static void main(String[] args) {
        RestTemplate template = new RestTemplate();
        System.out.println(template.getForEntity("http://google.com",
                String.class));
    }
}

✒️️ Edit  ⏰ History  🗑 Delete