330 lines
12 KiB
Java
330 lines
12 KiB
Java
package seed.com.gtm.util;
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.DataInputStream;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
import org.apache.ibatis.logging.Log;
|
|
import org.apache.ibatis.logging.LogFactory;
|
|
|
|
import seed.utils.ApplicationProperty;
|
|
|
|
public class Nice {
|
|
protected Log log = LogFactory.getLog(this.getClass());
|
|
|
|
public Nice() throws Exception {
|
|
}
|
|
|
|
private String getCryptoMD5String(String inputValue) throws NoSuchAlgorithmException,IOException {
|
|
byte[] ret = digest("MD5", inputValue.getBytes());
|
|
String result = encode(ret);
|
|
return result;
|
|
}
|
|
private byte[] digest(String alg, byte[] input)
|
|
throws NoSuchAlgorithmException {
|
|
MessageDigest md = MessageDigest.getInstance(alg);
|
|
return md.digest(input);
|
|
}
|
|
|
|
private String encode(byte[] encodeBytes) throws IOException {
|
|
byte[] buf = Base64.encodeBase64(encodeBytes);
|
|
return new String(buf).trim();
|
|
}
|
|
|
|
private String NiceApikey() throws Exception{
|
|
String uid = ApplicationProperty.get("nice.uid");
|
|
String pwd = ApplicationProperty.get("nice.pwd");
|
|
String s = uid + getCryptoMD5String(pwd)+ new SimpleDateFormat("yyyyMMdd").format(new Date());
|
|
//JAVA POST 전송시 UTF-8로 인코딩하면 APIKEY 인증오류남
|
|
//String apikey = URLEncoder.encode( getCryptoMD5String(s) ,"UTF-8");
|
|
String apikey = getCryptoMD5String(s);
|
|
return apikey;
|
|
}
|
|
|
|
|
|
public StringBuilder Lookup(String bizno) throws Exception{
|
|
int serverResponseCode = 0;
|
|
String serverResponseMessage = null;
|
|
|
|
HttpURLConnection connection = null;
|
|
DataOutputStream outputStream = null;
|
|
DataInputStream inputStream = null;
|
|
StringBuilder html = new StringBuilder();
|
|
|
|
String urlServer = "https://w.datapoint.co.kr/v2.0/lookup/lookup.info";
|
|
String lineEnd = "\r\n";
|
|
String twoHyphens = "--";
|
|
String boundary = "*****";
|
|
|
|
try
|
|
{
|
|
URL url = new URL(urlServer);
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// Allow Inputs & Outputs
|
|
connection.setDoInput(true);
|
|
connection.setDoOutput(true);
|
|
connection.setUseCaches(false);
|
|
|
|
// Enable POST method
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setRequestProperty("Connection", "Keep-Alive");
|
|
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
|
|
outputStream = new DataOutputStream( connection.getOutputStream() );
|
|
|
|
// params 생성.
|
|
//서비스상품코드
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"allsvcpdtcd\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
/*outputStream.writeBytes(URLEncoder.encode("KLP", "UTF-8"));*/
|
|
outputStream.writeBytes("KLP");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//이용 등록을 통해 받은 API키 스트링을 입력합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"apikey\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes(NiceApikey());
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//이용허가를 받은 구분코드 입니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"eprdatasvcstscd\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("07");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//이용 등록한 사용자ID를 입력합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"uid\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes(ApplicationProperty.get("nice.uid"));
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//사업자번호
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"bizno\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes(bizno);
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//출력방식 xml방식과 json 방식을 지정합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"output\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("json");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//호출할 세세분류ID 코드를 지정합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"resgp\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("LP0202,LP0232,LP0212,LP0209");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
|
|
|
// Responses from the server (code and message)
|
|
serverResponseCode = connection.getResponseCode();
|
|
serverResponseMessage = connection.getResponseMessage();
|
|
|
|
outputStream.flush();
|
|
outputStream.close();
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); //뒤에 charset으로 불러오기. 한글깨짐현상 방지.
|
|
for(;;){
|
|
String line = br.readLine();
|
|
if(line == null) break;
|
|
html.append(line.trim().replaceAll(" |<!--(.*)-->|@", ""));
|
|
}
|
|
br.close();
|
|
//params.put("nice", html);
|
|
|
|
}
|
|
catch (Exception ex){
|
|
log.warn("error>>nice>>||"+ex.toString());
|
|
}
|
|
return html;
|
|
}
|
|
|
|
|
|
public StringBuilder Search(String nm, String page) throws Exception{
|
|
int serverResponseCode = 0;
|
|
String serverResponseMessage = null;
|
|
|
|
HttpURLConnection connection = null;
|
|
DataOutputStream outputStream = null;
|
|
DataInputStream inputStream = null;
|
|
StringBuilder html = new StringBuilder();
|
|
|
|
String urlServer = "https://w.datapoint.co.kr/v2.0/search/company.info";
|
|
String lineEnd = "\r\n";
|
|
String twoHyphens = "--";
|
|
String boundary = "*****";
|
|
|
|
try
|
|
{
|
|
URL url = new URL(urlServer);
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
|
|
// Allow Inputs & Outputs
|
|
connection.setDoInput(true);
|
|
connection.setDoOutput(true);
|
|
connection.setUseCaches(false);
|
|
|
|
// Enable POST method
|
|
connection.setRequestMethod("POST");
|
|
|
|
connection.setRequestProperty("Connection", "Keep-Alive");
|
|
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
|
|
outputStream = new DataOutputStream( connection.getOutputStream() );
|
|
|
|
// params 생성.
|
|
//이용 등록을 통해 받은 API키 스트링을 입력합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"apikey\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes(NiceApikey());
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//이용허가를 받은 구분코드 입니다. (고정 : 07)
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"eprdatasvcstscd\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("07");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//사용자ID
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"uid\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes(ApplicationProperty.get("nice.uid"));
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//출력방식 xml방식과 json 방식을 지정합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"output\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("json");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//호출할 세세분류ID 코드를 지정합니다.
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"index\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
outputStream.writeBytes("SA0701");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//검색결과 출력건수 최대20
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"prn_rst_cnt\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
//((String) params.get("bizno")).trim().replaceAll("-", "");
|
|
outputStream.writeBytes("20");
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//검색의 시작위치(페이징 1~50 최대50제한)
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"pge_st_no\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
//((String) params.get("bizno")).trim().replaceAll("-", "");
|
|
outputStream.writeBytes(page);
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
//기업명 검색어
|
|
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
|
|
outputStream.writeBytes("Content-Disposition: form-data; name=\"nm\""+ lineEnd);
|
|
outputStream.writeBytes(lineEnd);
|
|
//NICE API에서 UTF-8을 못받으므로 8859_1로 보내야함
|
|
outputStream.writeBytes(new String(nm.getBytes("UTF-8"), "8859_1"));
|
|
outputStream.writeBytes(lineEnd);
|
|
|
|
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
|
|
|
|
// Responses from the server (code and message)
|
|
serverResponseCode = connection.getResponseCode();
|
|
serverResponseMessage = connection.getResponseMessage();
|
|
|
|
outputStream.flush();
|
|
outputStream.close();
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); //뒤에 charset으로 불러오기. 한글깨짐현상 방지.
|
|
for(;;){
|
|
String line = br.readLine();
|
|
if(line == null) break;
|
|
html.append(line.trim().replaceAll(" |<!--(.*)-->|@", ""));
|
|
}
|
|
br.close();
|
|
//params.put("nice", html);
|
|
|
|
}
|
|
catch (Exception ex){
|
|
log.warn("error>>nice>>||"+ex.toString());
|
|
}
|
|
return html;
|
|
}
|
|
|
|
public StringBuilder Search2(String nm, String page) throws Exception {
|
|
String encoded_query = URLEncoder.encode(nm, "UTF-8");
|
|
String host = "https://w.datapoint.co.kr/v3.1/search/company.info?apikey=" + this.NiceApikey() + "&uid=kofair&index=SA0701&eprdatasvcstscd=07&itg_srch=" + encoded_query + "&prn_rst_cnt=20&pge_st_no=" + page + "&output=json";
|
|
URL url = new URL(host);
|
|
StringBuilder html = new StringBuilder();
|
|
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setDoInput(true);
|
|
connection.setDoOutput(true);
|
|
connection.setUseCaches(false);
|
|
new StringBuilder();
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
|
|
|
while(true) {
|
|
String line = br.readLine();
|
|
if (line == null) {
|
|
br.close();
|
|
return html;
|
|
}
|
|
|
|
html.append(line.trim().replaceAll(" |<!--(.*)-->|@", ""));
|
|
}
|
|
}
|
|
|
|
public StringBuilder Lookup2(String bizno) throws Exception {
|
|
String host = "https://w.datapoint.co.kr/v2.0/lookup/lookup.info?apikey=" + this.NiceApikey() + "&uid=kofair&resgp=LP0202,LP0209,LP0212,LP0232&allsvcpdtcd=KLP&eprdatasvcstscd=07&bizno=" + bizno + "&output=json";
|
|
URL url = new URL(host);
|
|
StringBuilder html = new StringBuilder();
|
|
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
|
|
connection.setRequestMethod("GET");
|
|
connection.setDoInput(true);
|
|
connection.setDoOutput(true);
|
|
connection.setUseCaches(false);
|
|
new StringBuilder();
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
|
|
|
|
while(true) {
|
|
String line = br.readLine();
|
|
if (line == null) {
|
|
br.close();
|
|
return html;
|
|
}
|
|
|
|
html.append(line.trim().replaceAll(" |<!--(.*)-->|@", ""));
|
|
}
|
|
}
|
|
|
|
}
|