ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Server에서 다른 api 호출
    Java & 배경지식/Java 개발에 도움되는 코드들.. 2020. 7. 27. 14:29
    반응형

     

    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.io.BufferedReader;
    import java.io.IOException;
    
    public class ApiExplorer {
        public static void main(String[] args) throws IOException {
            StringBuilder urlBuilder = new StringBuilder("http://naver.com"); /*URL*/
            String key = "1234";
            urlBuilder.append("?" + URLEncoder.encode("key","UTF-8") + key); 
            urlBuilder.append("&" + URLEncoder.encode("test","UTF-8") + "=" + URLEncoder.encode("1", "UTF-8")); 
            
            URL url = new URL(urlBuilder.toString());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Content-type", "application/json");
    
            BufferedReader rd;
            if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
                rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            } else {
                rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
            }
            
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
            conn.disconnect();
            System.out.println(sb.toString());
        }
    }
    반응형

    'Java & 배경지식 > Java 개발에 도움되는 코드들..' 카테고리의 다른 글

    Java Server에서 페이징 Html 태그 그리기  (0) 2020.07.24
    List<VO> 정렬 DESC , ASC(2차 보완 완료)  (0) 2020.07.05
    poi  (0) 2020.04.26
    sftp 연결  (0) 2020.03.23
    IP 가져오기  (0) 2020.03.18
Designed by Tistory.