sitelink1 | http://lonelycat.tistory.com/314 |
---|---|
sitelink2 | |
sitelink3 | |
sitelink4 | |
sitelink5 | |
sitelink6 |
public String getMcodeFromMLB() {
HttpURLConnection uc = null; // 연결용 커넥션
URL url = null;
String sUrl = "http://localhost:8080/MLBServerTest/MLBTest.jsp"; // 연결할 주소
String result = "";
try {
url = new URL(sUrl);
uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestMethod("POST");
uc.setConnectTimeout(10000); // 커넥션 타임아웃
uc.setAllowUserInteraction(true);
// Http Header Setting
uc.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=euc-kr");
// Http Parameter Sending
String partner_id = "O00128"; // CP의 파트너ID
String service_code = "001"; // 각 CP서버에 할당된 코드
String music_code = "00293876"; // MLB에서 조회된 MUSIC_CODE
String content_price = "800"; // 컨텐츠 단가
String content_name = "핑계"; // 컨텐츠가 서비스되는 이름
String content_num = "1"; // 패키지에 포함한 컨텐츠의 갯수. 단일상품일 경우 1, 복합상품일 경우 2이상
String pid = "3102306023"; // PID
String sub_code = "12700000012"; // CP서버가 자체적으로 관리하는 Code(SCID등). 12자리 이하
String carrier_code = "00"; // ASP에 서비스하는 캐리어를 구분하기 위한 구분코드
StringBuffer sb = new StringBuffer();
sb.append(MLBConstants.MCODE_REQUEST_PARTNER_ID).append("=").append(partner_id).append("&");
sb.append(MLBConstants.MCODE_REQUEST_SERVICE_CODE).append("=").append(service_code).append("&");
sb.append(MLBConstants.MCODE_REQUEST_MUSIC_CODE).append("=").append(music_code).append("&");
sb.append(MLBConstants.MCODE_REQUEST_CONTENT_PRICE).append("=").append(content_price).append("&");
sb.append(MLBConstants.MCODE_REQUEST_CONTENT_NAME).append("=").append(content_name).append("&");
sb.append(MLBConstants.MCODE_REQUEST_CONTENT_NUM).append("=").append(content_num).append("&");
sb.append(MLBConstants.MCODE_REQUEST_PID).append("=").append(pid).append("&");
sb.append(MLBConstants.MCODE_REQUEST_SUB_CODE).append("=").append(sub_code).append("&");
sb.append(MLBConstants.MCODE_REQUEST_CARRIER_CODE).append("=").append(carrier_code);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(uc.getOutputStream(), "euc-kr"));
pw.write(sb.toString());
pw.flush();
int resCode = 0; // RMS 와의 연결 응답값
resCode = uc.getResponseCode();
StringBuffer resp = new StringBuffer();
if(resCode < 400){ // 연결이 성공적일때
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream(), "euc-kr"));
while ((line = br.readLine()) != null) {
System.out.println(line);
resp.append(line);
}
pw.close();
br.close();
// html 파싱
result = getResultCode(resp.toString());
result = (result.equals("OK")) ? result : "MLB연동 중 에러 발생 : " + getResultCode(resp.toString());
} else {
result = "MLB연동 중 에러 발생 : " + resCode + " 에러입니다.";
}
} catch (IOException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
return result;
}