티스토리 뷰
728x90
반응형
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 | public static String execute(String method, final String url, final JSONObject parameters) { HttpURLConnection connection = null; String result =""; int methodCase = 0; if(method.toUpperCase().equals("POST")) methodCase = 1; try { switch (methodCase) { case 0: connection = openConnection(url.concat("?").concat(formEncode(parameters))); connection.setRequestMethod("GET"); connection.setConnectTimeout(3000); connection.connect(); break; case 1: connection = openConnection(url); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); connection.setConnectTimeout(3000); connection.connect(); final OutputStream out = connection.getOutputStream(); out.write(parameters.toString().getBytes()); out.flush(); out.close(); break; } final int statusCode = connection.getResponseCode(); if (statusCode / 100 != 2) { // 400, 401, 501 result = "{\"error\":"+statusCode+"}"; }else{ result = readInputStream(connection.getInputStream()); } } catch (IOException e) { Logger.getLogger( HttpJsonUtil.class ).error( "HttpJsonUtil.execute--Error : ", e); return result; } finally { if (connection != null) connection.disconnect(); } return result; } ex) class.execute("POST", "http://127.0.0.1:8080/test/test1", null) | cs |
728x90
반응형
'Java' 카테고리의 다른 글
[Java] splite limit 이용 (0) | 2020.04.06 |
---|---|
[Java & JavaScript] Java에서 JSONObject에 htmlCode를 담아 프론트로 내려주고 JSON.parse() 메소드 사용시 에러날때 (0) | 2020.04.06 |
[Java] HttpURLConnection을 이용한 multipart request 전송 (0) | 2020.04.06 |
[Java] json to map, map to json 등 (0) | 2020.04.06 |
[Java] UUID 생성 (0) | 2020.04.06 |
댓글