77import java .net .URL ;
88import java .nio .charset .Charset ;
99import java .util .HashMap ;
10+ import java .util .Map ;
1011import java .util .Map .Entry ;
1112
1213import javax .net .ssl .HttpsURLConnection ;
1314
14- import org .apache .http .client .ClientProtocolException ;
15- import org .apache .http .client .methods .CloseableHttpResponse ;
16- import org .apache .http .client .methods .HttpDelete ;
17- import org .apache .http .client .methods .HttpPost ;
18- import org .apache .http .entity .AbstractHttpEntity ;
19- import org .apache .http .entity .ByteArrayEntity ;
20- import org .apache .http .entity .StringEntity ;
21- import org .apache .http .impl .client .CloseableHttpClient ;
22- import org .apache .http .impl .client .HttpClients ;
23-
2415import com .bitso .exceptions .BitsoAPIException ;
2516
2617import com .bitso .helpers .Helpers ;
18+ import org .apache .hc .client5 .http .ClientProtocolException ;
19+ import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
20+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
21+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
22+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
23+ import org .apache .hc .client5 .http .impl .classic .HttpClients ;
24+ import org .apache .hc .core5 .http .ContentType ;
25+ import org .apache .hc .core5 .http .io .entity .AbstractHttpEntity ;
26+ import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
27+ import org .apache .hc .core5 .http .io .entity .StringEntity ;
2728
2829public class BlockingHttpClient {
2930 private boolean log = false ;
@@ -102,12 +103,12 @@ public String sendPost(String url, String body, HashMap<String, String> headers,
102103 }
103104
104105 public String sendPost (String url , byte [] body , HashMap <String , String > headers )
105- throws ClientProtocolException , IOException {
106- return sendPost (url , new ByteArrayEntity (body ), headers );
106+ throws IOException {
107+ return sendPost (url , new ByteArrayEntity (body , 0 , body . length , ContentType . APPLICATION_JSON ), headers );
107108 }
108109
109110 private String sendPost (String url , AbstractHttpEntity body , HashMap <String , String > headers )
110- throws ClientProtocolException , IOException {
111+ throws IOException {
111112 throttle ();
112113
113114 HttpPost postRequest = new HttpPost (url );
@@ -119,13 +120,13 @@ private String sendPost(String url, AbstractHttpEntity body, HashMap<String, Str
119120
120121 postRequest .setEntity (body );
121122
122- CloseableHttpResponse closeableHttpResponse = HttpClients .createDefault (). execute ( postRequest );
123- String response = Helpers . convertInputStreamToString ( closeableHttpResponse . getEntity (). getContent () );
124-
125- return response ;
123+ try ( CloseableHttpClient client = HttpClients .createDefault ()) {
124+ CloseableHttpResponse response = client . execute ( postRequest );
125+ return Helpers . convertInputStreamToString ( response . getEntity (). getContent ());
126+ }
126127 }
127128
128- public String sendDelete (String url , HashMap <String , String > headers ) throws BitsoAPIException {
129+ public String sendDelete (String url , Map <String , String > headers ) throws BitsoAPIException {
129130 throttle ();
130131 HttpDelete deleteURL = new HttpDelete (url );
131132
@@ -135,10 +136,8 @@ public String sendDelete(String url, HashMap<String, String> headers) throws Bit
135136 }
136137 }
137138
138- CloseableHttpClient closeableHttpClient = HttpClients .createDefault ();
139- CloseableHttpResponse response = null ;
140- try {
141- response = closeableHttpClient .execute (deleteURL );
139+ try (CloseableHttpClient closeableHttpClient = HttpClients .createDefault ()) {
140+ CloseableHttpResponse response = closeableHttpClient .execute (deleteURL );
142141 return Helpers .convertInputStreamToString (response .getEntity ().getContent ());
143142 } catch (ClientProtocolException e ) {
144143 e .printStackTrace ();
0 commit comments