develog

[Java] http 인증 접속 본문

Dev/Java

[Java] http 인증 접속

냐옴 2014. 9. 18. 11:27

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.URL;

import java.net.URLConnection;


import org.apache.commons.codec.binary.Base64;


public class TestMain {


    public static void main(String[] args) throws Exception {

    BufferedReader br = null;

        try {

        String idpw = "username:password";

            String encodedIdPw = new String(Base64.encodeBase64(idpw.getBytes()));

            

            URL url = new URL("http://127.0.0.1/");

            URLConnection urlConn = url.openConnection();

            urlConn.setRequestProperty("Authorization", "Basic " + encodedIdPw);

            urlConn.connect();

            

            br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

String line = null;

while ((line = br.readLine()) != null) {

System.out.println(line);

}

        } finally {

        if (br != null) {

br.close();

}

        }

    }


}

'Dev > Java' 카테고리의 다른 글

[Java] Java IO  (0) 2014.09.22
[Java] removeTag  (0) 2014.09.18
[Java] System.getProperties(), System.getProperty()  (0) 2014.09.15
memory  (0) 2014.09.10
eclipse.ini  (0) 2014.09.10
Comments