Apache HTTP. Maven Dependency
<dependency>
<groupId> org.apache.httpcomponents</groupId>
<artifactId> httpclient</artifactId>
<version> 4.5.1</version>
</dependency>
HTTP Interface
Our goal is to implement Http interface that can be applicable for any purposes with minimum tuning. This interface should look like this
import java.util.Map ;
public interface Http {
GeneralResponse sendGet ( String url , Map < String , String > headers );
GeneralResponse sendPost ( String url , Map < String , String > headers , Map < String , String > headers );
GeneralResponse sendDelete ( String url , Map < String , String > headers );
GeneralResponse sendPut ( String url , Map < String , String > headers );
}
Response class
GeneralResponse consists of the statusLine (response code and phrase) and the content itself from the response.
Usually it’s json , atleast for me.
import org.apache.http.StatusLine ;
public class GeneralResponse {
public StatusLine statusLine ;
public String jsonContent ;
public GeneralResponse () {
}
public GeneralResponse ( StatusLine statusLine , String content ) {
this . statusLine = statusLine ;
this . jsonContent = content ;
}
}
Implementation of Http Interface
Here is the implementation of sendGet and sendPost methods of the Http interface.
SendDelete and sendPut look exactly like sendGet or sendPost just use HttpDelete and HttpPut .
import org.apache.http.Header ;
import org.apache.http.HttpEntity ;
import org.apache.http.HttpResponse ;
import org.apache.http.NameValuePair ;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.method.HttpGet ;
import org.apache.http.client.method.HttpPost ;
import org.apache.http.client.method.HttpDelete ;
import org.apache.http.client.method.HttpPut ;
import org.apache.http.entity.StringEntity ;
import org.apache.http.impl.client.HttpClientBuilder ;
import org.apache.http.message.BasicHeader ;
import org.apache.http.message.BasicNameValuePair ;
public class HttpHandler implements Http {
public GeneralResponse sendGet ( String url , Map < String , String > headers ) {
HttpGet httpGet = new HttpGet ( url );
String result = null ;
HttpResponse response = null ;
httpGet . setHeaders ( getHeadersArray ( headers ));
try {
response = HttpClientBuilder . create (). build (). execute ( httpGet );
result = getContentFromResponse ( response );
} catch ( Exception e ) {
e . printStackTrace ();
// TODO some actions that you need
}
returns new GeneralResponse ( response . getStatusLine (), result );
}
public GeneralResponse sendPost ( String url , Map < String , String > headers ,
Map < String , String > body ) {
HttpPost httpPost = new HttpPost ( url );
String result = null ;
HttpResponse response = null ;
httpPost . setHeaders ( getHeadersArray ( headers ));
try {
if ( body != null ) httpPost . setEntity ( getParamsEntity ( body ));
response = HttpClientBuilder . create (). build (). execute ( httpPost );
result = getContentFromResponse ( response );
} catch ( Exception e ) {
e . printStackTrace ();
// TODO some actions that you need
}
returns new GeneralResponse ( response . getStatusLine (), result );
}
private Header [] getHeadersArray ( Map < String , String > headersMap ) {
List < Header > headerList = new ArrayList < Header >();
for ( Map . Entry < String , String > header : headersMap . entrySet ()) {
headerList . add ( new BasicHeader ( header . getKey ), header . getValue ()));
}
Header [] headers = new Header [ headerList . size ()];
headerList . toArray ( headers );
return headers ;
}
private HttpEntity getParamsEntity ( Map < String , String > body )
throws UnsupportedEncodingException {
if ( body . containsKey ( "json" ) return new StringEntity ( body . get ( "json" ));
List < NameValuePair > params = new ArrayList < NameValuePair >();
for ( Map . Entry < String , String > param : body . entrySet ()) {
params . add ( new BasicNameValuePair ( param . getKey (), param . getValue ()));
}
return new UrlEncodedFormEntity ( params , "UTF-8" );
}
private String getContentFromResponse ( HttpResponse response ) throws IOException {
String result = "" ;
String line = null ;
BufferedReader in =
new BufferedReader ( new InputStreamReader ( response . getEntity (). getContent ()));
while (( line = in . readLine ()) != null ) {
result += line ;
}
in . close ();
return result ;
}
}
Usage example
@Autowired
Http http ;
...
GeneralResponse generalResponse = http . sendPost ( url , headers , body );
int code = generalResponse . statusLine . getStatusCode ();
String phrase = generalResponse . statusLine . getReasonPhrase ();
if ( code != 200 ) {
System . out . println ( "Ops, something wrong... " + code + " " + phrase );
} else {
Gson gson = new GsonBuilder (). create ();
person = gson . fromJson ( generalResponse . jsonContent , Person . class );
}
Need more? - Just google it
You may also find these posts interesting: