// RESTAPI 통신을 위한 라이브러리
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
// 엑셀 파일을 읽기 위한 라이브러리
import java.io.FileInputStream;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class ResponseTest02 {
public static void main(String[] args) {
ArrayList<String> urlList = new ArrayList<String>();
// 엑셀 파일 읽기
try {
FileInputStream file = new FileInputStream("D:/final/url0.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(file);
int rowindex=0;
int columnindex=0;
//시트 수 (첫번째에만 존재하므로 0을 준다)
//만약 각 시트를 읽기위해서는 FOR문을 한번더 돌려준다
XSSFSheet sheet=workbook.getSheetAt(0);
//행의 수
int rows=sheet.getPhysicalNumberOfRows();
for(rowindex=0;rowindex<=rows;rowindex++){
//행을읽는다
XSSFRow row=sheet.getRow(rowindex);
if(row !=null){
//셀의 수
int cells=row.getPhysicalNumberOfCells();
for(columnindex=0; columnindex<=cells; columnindex++){
//셀값을 읽는다
XSSFCell cell=row.getCell(columnindex);
String value="";
//셀이 빈값일경우를 위한 널체크
if(cell==null){
continue;
}else{
//타입별로 내용 읽기
switch (cell.getCellType()){
case XSSFCell.CELL_TYPE_FORMULA:
value=cell.getCellFormula();
break;
case XSSFCell.CELL_TYPE_NUMERIC:
value=cell.getNumericCellValue()+"";
break;
case XSSFCell.CELL_TYPE_STRING:
value=cell.getStringCellValue()+"";
break;
case XSSFCell.CELL_TYPE_BLANK:
value=cell.getBooleanCellValue()+"";
break;
case XSSFCell.CELL_TYPE_ERROR:
value=cell.getErrorCellValue()+"";
break;
}
}
urlList.add(value); // 엑셀 값을 리스트에 담기
}
}
}
}catch(Exception e) {
e.printStackTrace();
}
ArrayList<urlResponse> responseList = new ArrayList<urlResponse>(); // urlResponse 클래스 객체 리스트 생성
for (int i = 0; i < urlList.size(); i++) {
try {
// SSL 인증 문제 해결을 위해 추가 -----------------------------------------
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// -----------------------------------------------------------------------
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL url = new URL(urlList.get(i));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36");
int result = conn.getResponseCode(); // 상태코드
if (result >= 200 && result < 300) { // 2xx => 요청을 서버가 정상적으로 처리
responseList.add(new urlResponse(urlList.get(i), " : 성공", result, null));
//System.out.println(i + " " + result);
} else if (result >= 300 && result < 500) {
// 리디렉션 주소를 얻기 위해 추가한 코드 ----------------------
URLConnection con = new URL(urlList.get(i)).openConnection();
URL redirectUrl = getFinalURL(con.getURL());
//System.out.println(i + " " + result + " " + redirectUrl);
responseList.add(new urlResponse(urlList.get(i), "리디렉션", result, redirectUrl));
// ------------------------------------------------------
} else { // 200번대가 아니면 => 요청을 서버가 정상적으로 처리 X
responseList.add(new urlResponse(urlList.get(i), " : 실패", result, null));
// 리디렉션 주소를 얻기 위해 추가한 코드 ----------------------
URLConnection con = new URL(urlList.get(i)).openConnection();
URL redirectUrl = getFinalURL(con.getURL());
//System.out.println(i + " " + result + " " + redirectUrl);
// ------------------------------------------------------
}
} catch (Exception e) {
//System.out.println("Error: " + e.getMessage());
responseList.add(new urlResponse(urlList.get(i), " : 예외처리", 0, null));
//System.out.println(i + " ");
}
}
for (int j = 0; j < responseList.size(); j++) {
//System.out.println(responseList.get(j).result);
//System.out.println(j + "번째 결과 응답코드 : " + responseList.get(j).result);
System.out.println(j + " " + responseList.get(j).url + " " + responseList.get(j).result + " " + responseList.get(j).redirectUrl);
}
}
// URL과 응답코드를 함께 저장하기 위한 클래스
static class urlResponse {
String url;
String pf;
int result;
URL redirectUrl;
public urlResponse(String url, String pf, int result, URL redirectUrl) {
this.url = url;
this.pf = pf;
this.result = result;
this.redirectUrl = redirectUrl;
}
}
public static URL getFinalURL(URL url) {
try {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setInstanceFollowRedirects(false);
con.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36");
con.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
//con.addRequestProperty("Referer", "https://www.google.com/");
con.connect();
// Header에서 Status Code를 뽑는다.
int resCode = con.getResponseCode();
// http코드가 301(영구이동), 302(임시 이동), 303(기타 위치 보기), 403(접근 권리 없음), 404(요청받은 리소스 찾을 수 없음), 500(서버 오류) 이면 또다시 이 함수를 태운다. 재귀함수.
if (resCode == HttpURLConnection.HTTP_MOVED_PERM || resCode == HttpURLConnection.HTTP_MOVED_TEMP
|| resCode == HttpURLConnection.HTTP_SEE_OTHER || resCode == HttpURLConnection.HTTP_FORBIDDEN
|| resCode == HttpURLConnection.HTTP_NOT_FOUND || resCode == HttpURLConnection.HTTP_SERVER_ERROR
|| resCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
String Location = con.getHeaderField("Location");
if (Location.startsWith("/")) {
Location = url.getProtocol() + "://" + url.getHost() + Location;
}
return getFinalURL(new URL(Location));
}
} catch (Exception e) {
//System.out.println(e.getMessage());
}
return url;
}
}
Chrome : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
Edge : Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.56