간단하게 코드를 소개합니다.
흔히 쓰이는 HttpURLConnection ...
나만 자주 쓰낭;
무튼 이렇게해서 결과값을 읽어들이고 값에 따른 메시지를 출력하거나, 로그를 남기도록 처리했다.
흠 회사내에서 만든 코드를 퍼온거라 일부 내용들은 죄 삭제했다.
원래 쓰던거에선 좀더 안쪽에 복잡한 파서들이 즐비했는데..
태그를 써서 xml 로 데이터가 오지 않는한 그런건 필요없을듯 하다.
try {
URL url = new URL(urlString.toString());
URLConnection URLconnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode = httpConnection.getResponseCode(); //HTTP 응답 메세지 상태 코드를 가져옵니다.
if (responseCode == HttpURLConnection.HTTP_OK) { // HTTP_OK : HTTP Status-Code 200: 정상
InputStream is = httpConnection.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
byte[] buffer = new byte[512];
int len = 0;
while((len = dis.read(buffer, 0, buffer.length)) > 0){
byte[] req_byte = null;
req_byte = new byte[len];
for (int i = 0; i < len; i++) {
req_byte[i] = buffer[i];
}
resultString = new String(req_byte);
URL url = new URL(urlString.toString());
URLConnection URLconnection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
int responseCode = httpConnection.getResponseCode(); //HTTP 응답 메세지 상태 코드를 가져옵니다.
if (responseCode == HttpURLConnection.HTTP_OK) { // HTTP_OK : HTTP Status-Code 200: 정상
InputStream is = httpConnection.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
byte[] buffer = new byte[512];
int len = 0;
while((len = dis.read(buffer, 0, buffer.length)) > 0){
byte[] req_byte = null;
req_byte = new byte[len];
for (int i = 0; i < len; i++) {
req_byte[i] = buffer[i];
}
resultString = new String(req_byte);
resultString = resultString.trim();
//System.out.println("resultString : " + resultString);
}
} else {
// 오류 HTTP 연결실패
System.out.println("HTTP connection response != HTTP_OK");
}
// 결과값(resultString 에 따른 처리, xml 혹은 html 파일 자체를 읽어 처리할경우 파싱하는 프로세스를
// 추가로 만들어 사용해도 좋다.
if(resultString.startsWith("1001")) {
// 성공
} else if (resultString.startsWith("1000")) {
// 실패 => 오류코드 읽어들임.
} else {
// 실패 => 알수없는 오류
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
// 오류 HTTP 연결실패
System.out.println("HTTP connection response != HTTP_OK");
}
// 결과값(resultString 에 따른 처리, xml 혹은 html 파일 자체를 읽어 처리할경우 파싱하는 프로세스를
// 추가로 만들어 사용해도 좋다.
if(resultString.startsWith("1001")) {
// 성공
} else if (resultString.startsWith("1000")) {
// 실패 => 오류코드 읽어들임.
} else {
// 실패 => 알수없는 오류
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
흔히 쓰이는 HttpURLConnection ...
나만 자주 쓰낭;
무튼 이렇게해서 결과값을 읽어들이고 값에 따른 메시지를 출력하거나, 로그를 남기도록 처리했다.
흠 회사내에서 만든 코드를 퍼온거라 일부 내용들은 죄 삭제했다.
원래 쓰던거에선 좀더 안쪽에 복잡한 파서들이 즐비했는데..
태그를 써서 xml 로 데이터가 오지 않는한 그런건 필요없을듯 하다.