Maven Address
4.5. 2 --><dependency> <groupId>org.apache.commons</groupId> <artifactid >commons-lang3</artifactId> <version>3.6</version></dependency>< dependency> <groupId>org.apache.httpcomponents</groupId> <artifactid>httpclient </artifactId> <version>4.5. 2</version></dependency>
Httpclientutil method
PackageRichinfo.attendance.util;Importjava.io.IOException;ImportJava.net.URI;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Map;ImportOrg.apache.http.NameValuePair;Importorg.apache.http.client.entity.UrlEncodedFormEntity;ImportOrg.apache.http.client.methods.CloseableHttpResponse;ImportOrg.apache.http.client.methods.HttpGet;ImportOrg.apache.http.client.methods.HttpPost;ImportOrg.apache.http.client.utils.URIBuilder;ImportOrg.apache.http.entity.ContentType;Importorg.apache.http.entity.StringEntity;Importorg.apache.http.impl.client.CloseableHttpClient;Importorg.apache.http.impl.client.HttpClients;ImportOrg.apache.http.message.BasicNameValuePair;Importorg.apache.http.util.EntityUtils; Public classHttpclientutil { Public StaticString doget (string url, map<string, string>param) { //Create a HttpClient objectCloseablehttpclient httpclient =Httpclients.createdefault (); String resultstring= ""; Closeablehttpresponse Response=NULL; Try { //Create URIUriBuilder Builder =Newuribuilder (URL); if(param! =NULL) { for(String key:param.keySet ()) {Builder.addparameter (key, Param.get (key)); }} URI uri=Builder.build (); //Create an HTTP GET requestHttpGet HttpGet =NewHttpGet (URI); //Execute RequestResponse =Httpclient.execute (HttpGet); //determine if the return status is if(Response.getstatusline (). Getstatuscode () = = 200) {resultstring= Entityutils.tostring (Response.getentity (), "UTF-8"); } } Catch(Exception e) {e.printstacktrace (); } finally { Try { if(Response! =NULL) {response.close (); } httpclient.close (); } Catch(IOException e) {e.printstacktrace (); } } returnresultstring; } Public Staticstring doget (string url) {returnDoget (URL,NULL); } Public StaticString doPost (string url, map<string, string>param) { //Create a HttpClient objectCloseablehttpclient httpClient =Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String resultstring= ""; Try { //Create an HTTP POST requestHttpPost HttpPost =Newhttppost (URL); //Create a parameter list if(param! =NULL) {List<NameValuePair> paramlist =NewArraylist<>(); for(String key:param.keySet ()) {Paramlist.add (NewBasicnamevaluepair (Key, Param.get (key)); } //Simulation Formurlencodedformentity entity =Newurlencodedformentity (paramlist); Httppost.setentity (entity); } //executing an HTTP requestResponse =Httpclient.execute (HttpPost); Resultstring= Entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(Exception e) {e.printstacktrace (); } finally { Try{response.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnresultstring; } Public Staticstring doPost (string url) {returnDoPost (URL,NULL); } Public Staticstring Dopostjson (string URL, string json) {//Create a HttpClient objectCloseablehttpclient httpClient =Httpclients.createdefault (); Closeablehttpresponse Response=NULL; String resultstring= ""; Try { //Create an HTTP POST requestHttpPost HttpPost =Newhttppost (URL); //Create request Contentstringentity entity =Newstringentity (JSON, Contenttype.application_json); Httppost.setentity (entity); //executing an HTTP requestResponse =Httpclient.execute (HttpPost); Resultstring= Entityutils.tostring (Response.getentity (), "Utf-8"); } Catch(Exception e) {e.printstacktrace (); } finally { Try{response.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } returnresultstring; }}
HttpClient Tool class [pro-Test available]