화면에서 https --> http 방식으로 전달하면 브라우져에서 기본적으로 막는 현상이 있다고 하네요.
하기 전부터 불안불안했는데 ..
테스트할때는 http여서 인지를 못 하고 있었네요. ㅠㅠ
그래서 부랴부랴 http 방식으로 개발했어요.
방식은 GET방식으로 개발 햇어요.
타임아웃이 없으면 응답걸리는 동안 계속 로딩 중이여서 시간제한을 1초로 걸어놧어요.
가져다 쓰실때 메소드화 시켜서 파라메타 맞춰서 쓰시면 되고 url 부분 고치시고 ..
디버그 부분도 환경에 맞춰서 쓰시면 됩니다.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.io.IOException;
public class HttpGetWithTimeout {
public static void main(String[] args) {
String fullUrl = "http://example.com/api/data";
try {
URL url = new URL(fullUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// 타임아웃 설정 (ms)
conn.setConnectTimeout(1000); // 연결 시도 제한 시간
conn.setReadTimeout(1000); // 응답 대기 제한 시간
long startTime = System.currentTimeMillis();
int responseCode = conn.getResponseCode(); // 여기서 IOException 가능
long duration = System.currentTimeMillis() - startTime;
System.out.println("응답 시간: " + duration + "ms");
System.out.println("응답 코드: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8")
);
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine).append("\n");
}
in.close();
System.out.println("응답 내용:\n" + response.toString());
} else {
System.out.println("서버 응답 실패: HTTP 코드 = " + responseCode);
}
} catch (SocketTimeoutException e) {
System.err.println("⏱️ 요청 타임아웃 발생: " + e.getMessage());
} catch (UnknownHostException e) {
System.err.println("🌐 알 수 없는 호스트 (도메인 확인): " + e.getMessage());
} catch (IOException e) {
System.err.println("📡 통신 중 IO 오류: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
System.err.println("⚠️ 기타 오류 발생: " + e.getMessage());
e.printStackTrace();
}
}
}
'프로그램 > JAVA' 카테고리의 다른 글
java.util.regex.PatternSyntaxException: Illegal repetition near index 2#{TEST_NM} 자바오류 (0) | 2024.10.25 |
---|---|
Syntax error, annotations are only available if source level is 1.5 or greater (0) | 2024.07.05 |
이클립스 the project was not due to ~~ (0) | 2021.03.20 |
JAVA 버전별 다운로드 (0) | 2021.03.09 |
spring 프레임워크 Injection of autowired dependencies failed , could not autowire field 에러 (0) | 2020.06.26 |