{"id":1329,"date":"2017-10-10T18:28:59","date_gmt":"2017-10-10T18:58:59","guid":{"rendered":"https:\/\/mind4y.com\/itstuff\/?p=1329"},"modified":"2020-03-19T23:25:42","modified_gmt":"2020-03-19T23:55:42","slug":"java-http-simple-call","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=1329","title":{"rendered":"JAVA: HTTP &#8211; SIMPLE CALL"},"content":{"rendered":"<pre>class Base {\r\n\r\n\tprivate final String USER_AGENT = \"Mozilla\/5.0\";\r\n\r\n\t\r\n\tpublic void testConnection ( URL url){\r\n\t\ttry {\r\n\t\t\tHttpURLConnection con = (HttpURLConnection) url.openConnection();\r\n\t\t\t\r\n\t\t\t\/\/ optional default is GET\r\n\t\t\tcon.setRequestMethod(\"GET\");\r\n\t\t\t\r\n\t\t\t\/\/add request header\r\n\t\t\tcon.setRequestProperty(\"User-Agent\", USER_AGENT);\r\n\t\t\t\r\n\t\t\tint responseCode = con.getResponseCode();\r\n\t\t\tSystem.out.println(\"\\nSending 'GET' request to URL : \" + url);\r\n\t\t\tSystem.out.println(\"Response Code : \" + responseCode);\r\n\t\t\t\r\n\t\t\tBufferedReader in = new BufferedReader(\r\n\t\t\t\t\tnew InputStreamReader(con.getInputStream()));\r\n\t\t\tString inputLine;\r\n\t\t\tStringBuffer response = new StringBuffer();\r\n\t\t\t\r\n\t\t\twhile ((inputLine = in.readLine()) != null) {\r\n\t\t\t\tresponse.append(inputLine);\r\n\t\t\t}\r\n\t\t\tin.close();\r\n\t\t\t\r\n\t\t\t\/\/print result\r\n\t\t\tSystem.out.println(response.toString());\r\n\t\t} catch (MalformedURLException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t} catch (IOException e) {\r\n\t\t\t\/\/ TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n<\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>class Base { private final String USER_AGENT = &#8220;Mozilla\/5.0&#8221;; public void testConnection ( URL url){ try { HttpURLConnection con = (HttpURLConnection) url.openConnection(); \/\/ optional default is GET con.setRequestMethod(&#8220;GET&#8221;); \/\/add request header con.setRequestProperty(&#8220;User-Agent&#8221;, USER_AGENT); int responseCode = con.getResponseCode(); System.out.println(&#8220;\\nSending &#8216;GET&#8217; request to URL : &#8221; + url); System.out.println(&#8220;Response Code : &#8221; + responseCode); BufferedReader in = &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=1329\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;JAVA: HTTP &#8211; SIMPLE CALL&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1329","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1329"}],"version-history":[{"count":2,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/1329\/revisions\/2644"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}