{"id":330,"date":"2020-10-30T17:41:26","date_gmt":"2020-10-30T08:41:26","guid":{"rendered":"https:\/\/java.ihavenomoney.co.kr\/?p=330"},"modified":"2020-10-30T17:41:26","modified_gmt":"2020-10-30T08:41:26","slug":"java-httpurlconnection-get-post-%eb%a1%9c-%eb%82%a0%eb%a0%a4-%ea%b0%92-%eb%b0%9b%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/java.ihavenomoney.co.kr\/?p=330","title":{"rendered":"[java] HttpURLConnection Get post  \ub85c \ub0a0\ub824 \uac12 \ubc1b\uae30"},"content":{"rendered":"\n<pre class=\"lang:java decode:true \" >package test02;\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.io.OutputStream;\r\nimport java.net.HttpURLConnection;\r\nimport java.net.URL;\r\nimport java.io.PrintWriter;\r\nimport java.io.StringWriter;\r\n\r\n\r\npublic class test01 {\r\n\r\n\tprivate static final String USER_AGENT = \"Mozilla\/5.0\";\r\n\r\n\tprivate static final String GET_URL = \"https:\/\/data.auctionpro.co.kr\/airline?code=ke\";\r\n\tprivate static final String POST_URL = \"https:\/\/data.auctionpro.co.kr\/airline\";\r\n\r\n\tprivate static final String POST_PARAMS = \"code=oz\";\r\n\r\n\tpublic static void main(String[] args) throws IOException {\r\n\r\n\t\tsendGET();\r\n\t\tSystem.out.println(\"GET DONE\");\r\n\t\t\r\n\t\tsendPOST();\r\n\t\tSystem.out.println(\"POST DONE\");\r\n\t}\r\n\r\n\tprivate static void sendGET() throws IOException {\r\n\t\tURL obj = new URL(GET_URL);\r\n\t\tHttpURLConnection con = (HttpURLConnection) obj.openConnection();\r\n\r\n\t\ttry {\r\n\t\t\tcon.setRequestMethod(\"GET\");\r\n\t\t\tcon.setRequestProperty(\"User-Agent\", USER_AGENT);\r\n\t\t\tcon.setRequestProperty(\"Content-Type\", \"application\/x-www-form-urlencoded; charset=utf-8\");\r\n\t\t\tcon.setRequestProperty(\"Content-Length\", \"length\");\r\n\t        \/\/con.setRequestProperty(\"Accept-Charset\", \"UTF-8\"); \r\n\t\t\tint responseCode = con.getResponseCode();\r\n\t\t\t\r\n\t\t\t\/\/GET Response Code :: 200\r\n\t\t\tSystem.out.println(\"GET Response Code :: \" + responseCode);\r\n\t\t\t\r\n\t\t\tif (responseCode == HttpURLConnection.HTTP_OK) { \/\/ success\r\n\t\t\t\tBufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),\"UTF-8\"));\r\n\t\t\t\tString inputLine = null;\r\n\t\t\t\tStringBuffer response = new StringBuffer();\r\n\t\r\n\t\t\t\twhile ((inputLine = in.readLine()) != null) {\r\n\t\t\t\t\tresponse.append(inputLine).append(\"\\r\\n\");\r\n\t\t\t\t   }\r\n\t\t\t\tin.close();\r\n\t\t\t\t\r\n\t\t\t\t\/\/ print result\r\n\t\t\t\tSystem.out.println(response.toString());\r\n\t\t\t} else {\r\n\t\t\t\tSystem.out.println(\"GET request not worked\");\r\n\t\t\t}\r\n\t\t\r\n\t\t} catch (Exception e) {\r\n\t\t\tStringWriter sw = new StringWriter();\r\n\t\t\tPrintWriter pw = new PrintWriter(sw);\r\n\t\t\te.printStackTrace(pw);\r\n\t\t\tsw.toString(); \/\/ stack trace as a string\r\n\t\t\tSystem.out.println(sw.toString());\r\n\t\t\t\r\n        } finally {\r\n            if (con != null) {\r\n            \tcon.disconnect();\r\n            }\r\n        }\r\n\r\n\t}\r\n\r\n\tprivate static void sendPOST() throws IOException {\r\n\t\tURL obj = new URL(POST_URL);\r\n\t\tHttpURLConnection con = (HttpURLConnection) obj.openConnection();\r\n\t\t\r\n\t\ttry {\r\n\t\t\t\tcon.setRequestMethod(\"POST\");\r\n\t\t\t\tcon.setRequestProperty(\"User-Agent\", USER_AGENT);\r\n\t\t\r\n\t\t\t\t\/\/ For POST only - START\r\n\t\t\t\tcon.setDoOutput(true);\r\n\t\t\t\tOutputStream os = con.getOutputStream();\r\n\t\t\t\tos.write(POST_PARAMS.getBytes());\r\n\t\t\t\tos.flush();\r\n\t\t\t\tos.close();\r\n\t\t\t\t\/\/ For POST only - END\r\n\t\t\r\n\t\t\t\tint responseCode = con.getResponseCode();\r\n\t\t\t\tSystem.out.println(\"POST Response Code :: \" + responseCode);\r\n\t\t\r\n\t\t\t\tif (responseCode == HttpURLConnection.HTTP_OK) { \/\/success\r\n\t\t\t\t\tBufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),\"UTF-8\"));\r\n\t\t\t\t\tString inputLine;\r\n\t\t\t\t\tStringBuffer response = new StringBuffer();\r\n\t\t\r\n\t\t\t\t\twhile ((inputLine = in.readLine()) != null) {\r\n\t\t\t\t\t\tresponse.append(inputLine).append(\"\\r\\n\");;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tin.close();\r\n\t\t\r\n\t\t\t\t\t\/\/ print result\r\n\t\t\t\t\tSystem.out.println(response.toString());\r\n\t\t\t\t} else {\r\n\t\t\t\t\tSystem.out.println(\"POST request not worked\");\r\n\t\t\t\t}\r\n\t\t\t} catch (Exception e) {\r\n\t\t\t\tStringWriter sw = new StringWriter();\r\n\t\t\t\tPrintWriter pw = new PrintWriter(sw);\r\n\t\t\t\te.printStackTrace(pw);\r\n\t\t\t\tsw.toString(); \/\/ stack trace as a string\r\n\t\t\t\tSystem.out.println(sw.toString());\r\n\t\t\t\t\r\n\t\t    } finally {\r\n\t\t        if (con != null) {\r\n\t\t        \tcon.disconnect();\r\n\t\t        }\r\n\t\t    }\r\n\t}\r\n\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>package test02; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.io.PrintWriter; import java.io.StringWriter; public class test01 { private static final String USER_AGENT = &#8220;Mozilla\/5.0&#8221;; private static final String GET_URL = &#8220;https:\/\/data.auctionpro.co.kr\/airline?code=ke&#8221;; private static final String POST_URL = &#8220;https:\/\/data.auctionpro.co.kr\/airline&#8221;; private static final String POST_PARAMS = &#8220;code=oz&#8221;; public static void main(String[] args) &hellip;<br \/><a href=\"https:\/\/java.ihavenomoney.co.kr\/?p=330\" class=\"more-link pen_button pen_element_default pen_icon_arrow_double\"><span class=\"screen-reader-text\">[java] HttpURLConnection Get post  \ub85c \ub0a0\ub824 \uac12 \ubc1b\uae30<\/span> \ub354\ubcf4\uae30<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-jsp-"],"_links":{"self":[{"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=330"}],"version-history":[{"count":1,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":331,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/330\/revisions\/331"}],"wp:attachment":[{"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java.ihavenomoney.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}