캡챠 이미지 발급 소스
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}
Java - json 개요, Gson 사용 (0) | 2018.10.04 |
---|---|
Java - XML 파싱 후 List로 출력하기 (0) | 2018.10.04 |
Java - 네이버 단축URL API (0) | 2018.10.04 |
Java - 네이버 블로그 검색 API (0) | 2018.10.04 |
Java - URL 쿼리를 post 방식으로 전송하기 (0) | 2018.10.02 |
댓글 영역