{"id":143,"date":"2020-07-04T14:02:16","date_gmt":"2020-07-04T06:02:16","guid":{"rendered":"https:\/\/22pig.com\/?p=143"},"modified":"2020-07-04T14:02:16","modified_gmt":"2020-07-04T06:02:16","slug":"java-httpclientutil-java-http-client","status":"publish","type":"post","link":"http:\/\/22pig.com\/?p=143","title":{"rendered":"[java] HttpClientUtil.java -Http Client"},"content":{"rendered":"<p>package com.22pig.util;<\/p>\n<p>import java.io.BufferedReader;<br \/>\nimport java.io.IOException;<br \/>\nimport java.io.InputStreamReader;<br \/>\nimport java.io.PrintWriter;<br \/>\nimport java.net.URL;<br \/>\nimport java.net.URLConnection;<\/p>\n<p>import com.alibaba.fastjson.JSONObject;<\/p>\n<p>public class HttpClientUtil {<\/p>\n<p>public static String sendGet(String url, String getValueByKey) {<br \/>\nString result = sendGet(url);<br \/>\nJSONObject ticketObj =JSONObject.parseObject(result);<br \/>\nString value =ticketObj.getString(getValueByKey);<br \/>\nreturn value;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* \u5411\u6307\u5b9aURL\u53d1\u9001GET\u65b9\u6cd5\u7684\u8bf7\u6c42<br \/>\n*<br \/>\n* @param url<br \/>\n* \u53d1\u9001\u8bf7\u6c42\u7684URL<br \/>\n* @param param<br \/>\n* \u8bf7\u6c42\u53c2\u6570\uff0c\u8bf7\u6c42\u53c2\u6570\u5e94\u8be5\u662f name1=value1&amp;name2=value2 \u7684\u5f62\u5f0f\u3002<br \/>\n* @return URL \u6240\u4ee3\u8868\u8fdc\u7a0b\u8d44\u6e90\u7684\u54cd\u5e94\u7ed3\u679c<br \/>\n*\/<br \/>\npublic static String sendGet(String url) {<br \/>\nString result = &#8220;&#8221;;<br \/>\nBufferedReader in = null;<br \/>\ntry {<br \/>\nString urlNameString = url;\/\/+ &#8220;?&#8221; + param;<br \/>\nURL realUrl = new URL(urlNameString);<br \/>\n\/\/ \u6253\u5f00\u548cURL\u4e4b\u95f4\u7684\u8fde\u63a5<br \/>\nURLConnection connection = realUrl.openConnection();<br \/>\n\/\/ \u8bbe\u7f6e\u901a\u7528\u7684\u8bf7\u6c42\u5c5e\u6027<br \/>\nconnection.setRequestProperty(&#8220;accept&#8221;, &#8220;*\/*&#8221;);<br \/>\nconnection.setRequestProperty(&#8220;connection&#8221;, &#8220;Keep-Alive&#8221;);<br \/>\nconnection.setRequestProperty(&#8220;user-agent&#8221;,&#8221;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)&#8221;);<br \/>\n\/\/ \u5efa\u7acb\u5b9e\u9645\u7684\u8fde\u63a5<br \/>\nconnection.connect();<br \/>\n\/\/ \u83b7\u53d6\u6240\u6709\u54cd\u5e94\u5934\u5b57\u6bb5<br \/>\n\/\/Map&lt;String, List&lt;String&gt;&gt; map = connection.getHeaderFields();<br \/>\n\/\/ \u904d\u5386\u6240\u6709\u7684\u54cd\u5e94\u5934\u5b57\u6bb5<br \/>\n\/\/ for (String key : map.keySet()) {<br \/>\n\/\/ System.out.println(key + &#8220;&#8212;&gt;&#8221; + map.get(key));<br \/>\n\/\/ }<br \/>\n\/\/ \u5b9a\u4e49 BufferedReader\u8f93\u5165\u6d41\u6765\u8bfb\u53d6URL\u7684\u54cd\u5e94<br \/>\nin = new BufferedReader(new InputStreamReader(connection.getInputStream(),&#8221;UTF-8&#8243;));<br \/>\nString line;<br \/>\nwhile ((line = in.readLine()) != null) {<br \/>\nresult += line;<br \/>\n}<br \/>\n} catch (Exception e) {<br \/>\ne.printStackTrace();<br \/>\n}finally { \/\/ \u4f7f\u7528finally\u5757\u6765\u5173\u95ed\u8f93\u5165\u6d41<br \/>\ntry {<br \/>\nif (in != null) {<br \/>\nin.close();<br \/>\n}<br \/>\n} catch (Exception e2) {<br \/>\ne2.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>\/**<br \/>\n* \u5411\u6307\u5b9a URL \u53d1\u9001POST\u65b9\u6cd5\u7684\u8bf7\u6c42<br \/>\n*<br \/>\n* @param url<br \/>\n* \u53d1\u9001\u8bf7\u6c42\u7684 URL<br \/>\n* @param param<br \/>\n* \u8bf7\u6c42\u53c2\u6570\uff0c\u8bf7\u6c42\u53c2\u6570\u5e94\u8be5\u662f name1=value1&amp;name2=value2 \u7684\u5f62\u5f0f\u3002<br \/>\n* @return \u6240\u4ee3\u8868\u8fdc\u7a0b\u8d44\u6e90\u7684\u54cd\u5e94\u7ed3\u679c<br \/>\n*\/<br \/>\npublic static String sendPost(String url, String param) {<br \/>\nPrintWriter out = null;<br \/>\nBufferedReader in = null;<br \/>\nString result = &#8220;&#8221;;<br \/>\ntry {<br \/>\nURL realUrl = new URL(url);<br \/>\n\/\/ \u6253\u5f00\u548cURL\u4e4b\u95f4\u7684\u8fde\u63a5<br \/>\nURLConnection conn = realUrl.openConnection();<br \/>\n\/\/ \u8bbe\u7f6e\u901a\u7528\u7684\u8bf7\u6c42\u5c5e\u6027<br \/>\nconn.setRequestProperty(&#8220;accept&#8221;, &#8220;*\/*&#8221;);<br \/>\nconn.setRequestProperty(&#8220;connection&#8221;, &#8220;Keep-Alive&#8221;);<br \/>\nconn.setRequestProperty(&#8220;user-agent&#8221;,&#8221;Mozilla\/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)&#8221;);<br \/>\n\/\/ \u53d1\u9001POST\u8bf7\u6c42\u5fc5\u987b\u8bbe\u7f6e\u5982\u4e0b\u4e24\u884c<br \/>\nconn.setDoOutput(true);<br \/>\nconn.setDoInput(true);<br \/>\n\/\/ \u83b7\u53d6URLConnection\u5bf9\u8c61\u5bf9\u5e94\u7684\u8f93\u51fa\u6d41<br \/>\nout = new PrintWriter(conn.getOutputStream());<br \/>\n\/\/ \u53d1\u9001\u8bf7\u6c42\u53c2\u6570<br \/>\nout.print(param);<br \/>\n\/\/ flush\u8f93\u51fa\u6d41\u7684\u7f13\u51b2<br \/>\nout.flush();<br \/>\n\/\/ \u5b9a\u4e49BufferedReader\u8f93\u5165\u6d41\u6765\u8bfb\u53d6URL\u7684\u54cd\u5e94<br \/>\nin = new BufferedReader(new InputStreamReader(conn.getInputStream(),&#8221;UTF-8&#8243;));<br \/>\nString line;<br \/>\nwhile ((line = in.readLine()) != null) {<br \/>\nresult += line;<br \/>\n}<br \/>\n} catch (Exception e) {<br \/>\ne.printStackTrace();<br \/>\n}finally{\/\/\u4f7f\u7528finally\u5757\u6765\u5173\u95ed\u8f93\u51fa\u6d41\u3001\u8f93\u5165\u6d41<br \/>\ntry{<br \/>\nif(out!=null){<br \/>\nout.close();<br \/>\n}<br \/>\nif(in!=null){<br \/>\nin.close();<br \/>\n}<br \/>\n}<br \/>\ncatch(IOException ex){<br \/>\nex.printStackTrace();<br \/>\n}<br \/>\n}<br \/>\nreturn result;<br \/>\n}<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>package com.22pig.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import com.alibaba.fastjson.JSONObject; public class HttpClientUtil { public static String sendGet(String url, String getValueByKey) { String result = sendGet(url); JSONObject ticketObj =JSONObject.parseObject(result); String value =ticketObj.getString(getValueByKey); return value; } \/** * \u5411\u6307\u5b9aURL\u53d1\u9001GET\u65b9\u6cd5\u7684\u8bf7\u6c42 * * @param url * \u53d1\u9001\u8bf7\u6c42\u7684URL * @param param * \u8bf7\u6c42\u53c2\u6570\uff0c\u8bf7\u6c42\u53c2\u6570\u5e94\u8be5\u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":0,"href":"http:\/\/22pig.com\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/22pig.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}