상세 컨텐츠

본문 제목

Java - 네이버 캡챠 API 이미지 발급/수신/비교

개발/Java

by 뉴에이스 2018. 10. 4. 13:02

본문

캡챠 이미지 발급 소스

 

public static void main(String[] args) {
    String clientId = "xxxxx"; // 애플리케이션 클라이언트 아이디값";
    String clientSecret = "xxxxx"; // 애플리케이션 클라이언트 시크릿값";
    try {
        String code = "0"; // 키 발급시 0,  캡차 이미지 비교시 1로 세팅
        String apiURL = "https://openapi.naver.com/v1/captcha/nkey?code=" + code;
        URL url = new URL(apiURL);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("X-Naver-Client-Id", clientId);
        con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
        int responseCode = con.getResponseCode();
        BufferedReader br;
        if(responseCode==200) { // 정상 호출
            br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {  // 에러 발생
            br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        }
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = br.readLine()) != null) {
            response.append(inputLine);
        }
        br.close();
        System.out.println(response.toString());
    } catch (Exception e) {
        System.out.println(e);
    }
}

 

결과

 

{"key":"xxxxx"} // 발급된 이미지에 대한 Key

 

캡챠 이미지 수신

 

public static void main(String[] args) {
    String clientId = "xxxxx";//애플리케이션 클라이언트 아이디값";
    String clientSecret = "xxxxx";//애플리케이션 클라이언트 시크릿값";
    try {
        String key = "xxxxx"; // https://openapi.naver.com/v1/captcha/nkey 호출로 받은 키값
        String apiURL = "https://openapi.naver.com/v1/captcha/ncaptcha.bin?key=" + key;
        URL url = new URL(apiURL);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("X-Naver-Client-Id", clientId);
        con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
        int responseCode = con.getResponseCode();
        BufferedReader br;
        if(responseCode==200) { // 정상 호출
            InputStream is = con.getInputStream();
            int read = 0;
            byte[] bytes = new byte[1024];
            // 랜덤한 이름으로 파일 생성
            String tempname = Long.valueOf(new Date().getTime()).toString();
            File f = new File("data/" + tempname + ".jpg");
            f.createNewFile();
            OutputStream outputStream = new FileOutputStream(f);
            while ((read =is.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
            is.close();
        } else {  // 에러 발생
            br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = br.readLine()) != null) {
                response.append(inputLine);
            }
            br.close();
            System.out.println(response.toString());
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

 

발급받은 캡챠 이미지 (발급요청시바다 변경됨)

 

 

캡챠 이미지 비교

 

public static void main(String[] args) {
    String clientId = "xxxxx";//애플리케이션 클라이언트 아이디값";
    String clientSecret = "xxxxx";//애플리케이션 클라이언트 시크릿값";
    try {
        String code = "1"; // 키 발급시 0,  캡차 이미지 비교시 1로 세팅
        String key = "xxxx"; // 캡차 키 발급시 받은 키값
        String value = "X79A61N"; // 사용자가 입력한 캡차 이미지 글자값
        String apiURL = "https://openapi.naver.com/v1/captcha/nkey?code=" + code +"&key="+ key + "&value="+ value;

        URL url = new URL(apiURL);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("X-Naver-Client-Id", clientId);
        con.setRequestProperty("X-Naver-Client-Secret", clientSecret);
        int responseCode = con.getResponseCode();
        BufferedReader br;
        if(responseCode==200) { // 정상 호출
            br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {  // 에러 발생
            br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        }
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = br.readLine()) != null) {
            response.append(inputLine);
        }
        br.close();
        System.out.println(response.toString());
    } catch (Exception e) {
        System.out.println(e);
    }
}

 

결과

 

{"result":true,"responseTime":50.87}

 

관련글 더보기

댓글 영역