반응형
URL url = new URL(serviceURL);
URLConnection conn = url.openConnection();
String userCredentials = "username:password";
String basicAuth = " " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
// api 키 값 설정
conn.setRequestProperty ("Authorization", basicAuth);
conn.setRequestMethod("POST");
// Content-Type이 무엇인지 API 연동 업체에 꼭 문의해야 함.
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//conn.setRequestProperty("Content-Length", "" + postData.getBytes().length);
//conn.setRequestProperty("Content-Language", "en-US");
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
DataOutputStream osw = null;
BufferedReader br = null;
// response data
StringBuffer sbResponse = new StringBuffer();
boolean isSuccess = true;
try {
osw = new DataOutputStream(conn.getOutputStream());
// 서버로 전송할 데이터
osw.writeBytes("jsonData=" + URLEncoder.encode(param.toString(), "UTF-8"));
osw.flush();
br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sbResponse.append(line);
}
catch(Exception e) {
isSuccess = false;
}
finally {
if(null != osw) {
osw.close();
}
if(null != br) {
br.close();
}
}
728x90
반응형
'웹 개발' 카테고리의 다른 글
[java] https connection (0) | 2019.12.30 |
---|---|
java file transfer (0) | 2019.12.27 |
java에서 logback 사용 (0) | 2019.12.16 |
JSP / JSTL 에서 URL encode 하는 방법 (0) | 2019.11.21 |
jqGrid grid unload (0) | 2019.11.20 |