Apache HttpClient is an open source component provided by Apache that makes it easy to invoke HTTP requests using HttpClient. Since the 4.1 release, HttpClient's API has changed significantly, and many methods have been called differently than the 3.x version. This article uses the current latest version of 4.5.3.
First, the Httpcomponents dependency package is introduced in the Pom file:
1 <Dependency>2 <groupId>Org.apache.httpcomponents</groupId>3 <Artifactid>HttpClient</Artifactid>4 <version>4.5.3</version>5 </Dependency>
This article shows a POST request.
1 Importjava.io.IOException;2 3 Importorg.apache.commons.lang3.ObjectUtils;4 Importorg.apache.commons.lang3.StringUtils;5 Importorg.apache.http.Consts;6 ImportOrg.apache.http.HttpStatus;7 Importorg.apache.http.ParseException;8 ImportOrg.apache.http.client.config.RequestConfig;9 ImportOrg.apache.http.client.methods.CloseableHttpResponse;Ten ImportOrg.apache.http.client.methods.HttpPost; One ImportOrg.apache.http.entity.ContentType; A Importorg.apache.http.entity.StringEntity; - Importorg.apache.http.impl.client.CloseableHttpClient; - Importorg.apache.http.impl.client.HttpClients; the Importorg.apache.http.util.EntityUtils; - - /** - * @author + * - * @date May 18, 2017 morning 9:17:30 + * A * @Description at */ - Public classHttppostutils { - /** - * - * @paramURI - * The request address in * @paramJSON - * The request data that must is a JSON string to * @paramConnectTimeout + * The timeout in milliseconds until a connection is established - * @paramConnectionrequesttimeout the * The timeout in milliseconds used when requesting a connection * * From the connection Manager $ * @paramSockettimeoutPanax Notoginseng * The socket timeout in milliseconds, which are the timeout for - * Waiting for data or, put differently, a maximum period the * Inactivity between, consecutive data packets + * @returnNULL when method parameter is null, "", "" A * @throwsIOException the * If HTTP connection can not opened or closed successfully + * @throwsparseexception - * If response data can not parsed successfully $ */ $ PublicString Postjson (String uri, string json,intConnectTimeout,intConnectionrequesttimeout,intsockettimeout) - throwsIOException, ParseException { - if(Stringutils.isanyblank (URI, JSON)) { the return NULL; - }Wuyi theCloseablehttpclient client =Httpclients.createdefault (); -HttpPost post =NewHttpPost (URI); Wu //Create Request Data -stringentity entity =Newstringentity (JSON, Contenttype.application_json); About //Set Request Body $ post.setentity (entity); - -Requestconfig config =Requestconfig.custom (). Setconnecttimeout (ConnectTimeout) - . Setconnectionrequesttimeout (connectionrequesttimeout). SetSocketTimeout (Sockettimeout). build (); A post.setconfig (config); + //Response Content theString responsecontent =NULL; -Closeablehttpresponse response =NULL; $ Try { theResponse =Client.execute (POST); the if(Response.getstatusline (). Getstatuscode () = =HTTPSTATUS.SC_OK) { theResponsecontent =entityutils.tostring (Response.getentity (), Consts.UTF_8.name ()); the } -}finally { in if(Objectutils.anynotnull (response)) { the response.close (); the } About if(objectutils.anynotnull (client)) { the client.close (); the } the } + returnresponsecontent; - } the}
Using Apache HttpClient 4.x to send JSON data