Https랑 Http랑 사용 하는 방식은 같은데 URL에 따라 사용 하면 된다.
GET이랑 POST도 어짜피 동일해서 HttpURLConnection 만 GET 예제 만들었음.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; import javax.net.ssl.HttpsURLConnection; public class sampleTest { //샘플용 코드 제작 public void HttpURLConnectionGet(String strURL, String strParams) { try { URL url = new URL(strURL + "?" + strParams); //get 방식은 parameter를 URL에 묶어서 보낸다. HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setRequestProperty("Accept-Language", "ko-kr"); conn.setRequestProperty("Access-Control-Allow-Origin", "*"); conn.setRequestProperty("Content-Type", "application/json"); conn.setConnectTimeout(10000); // 커넥션 timeout 10초 conn.setReadTimeout(5000); //컨텐츠 조회시 timeout 5초 Charset charset = Charset.forName("UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String inputLine; StringBuffer response = new StringBuffer(); while((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); } catch (MalformedURLException e) { //URL e.printStackTrace(); } catch (IOException e) { // HttpURLConnection e.printStackTrace(); } } public void HttpURLConnectionPost(String strURL, String strParams) { try { URL url = new URL(strURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); //con.setRequestProperty("Authorization", alpha); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setRequestProperty("Accept-Language", "ko-kr"); conn.setRequestProperty("Access-Control-Allow-Origin", "*"); conn.setRequestProperty("Content-Type", "application/json"); conn.setConnectTimeout(10000); // 커넥션 timeout 10초 conn.setReadTimeout(5000); //컨텐츠 조회시 timeout 5초 conn.setDoOutput(true); //항상 갱신된 내용 가져오도록 true로 설정 DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(strParams); //파라미터 write wr.flush(); wr.close(); //int responseCode = conn.getResponseCode(); Charset charset = Charset.forName("UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String inputLine; StringBuffer response = new StringBuffer(); while((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } catch (MalformedURLException e) { //URL e.printStackTrace(); } catch (IOException e) { //HttpURLConnection e.printStackTrace(); } } public void HttpsURLConnectionPost(String strURL, String strParams) { //HttpsURLConnection은 HttpURLConnection을 상속 받는다. //사용 방식은 동일하나 http url과 https url이 다른 부분이다. try { URL url = new URL(strURL); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setRequestMethod("POST"); //con.setRequestProperty("Authorization", alpha); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setRequestProperty("Accept-Language", "ko-kr"); conn.setRequestProperty("Access-Control-Allow-Origin", "*"); conn.setRequestProperty("Content-Type", "application/json"); conn.setConnectTimeout(10000); // 커넥션 timeout 10초 conn.setReadTimeout(5000); //컨텐츠 조회시 timeout 5초 conn.setDoOutput(true); //항상 갱신된 내용 가져오도록 true로 설정 DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(strParams); //파라미터 write wr.flush(); wr.close(); //int responseCode = conn.getResponseCode(); Charset charset = Charset.forName("UTF-8"); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String inputLine; StringBuffer response = new StringBuffer(); while((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } catch (MalformedURLException e) { //URL e.printStackTrace(); } catch (IOException e) { //HttpsURLConnection e.printStackTrace(); } } public static void main(String[] args) { /* : 예제 만들때마다 새로 형태 만들기 귀찮아서 만들어서 사용하려고 제작하였음. 출처 : https://goddaehee.tistory.com/268 : https://bobr2.tistory.com/entry/HttpConnection-Get-Post-%EC%82%AC%EC%9A%A9%EB%B2%95 */ } } | cs |
깔끔하게 잘 써주셨습니다
답글삭제