Skip to content

Commit fa50ff7

Browse files
committed
httpclient5 has different packages
1 parent a11392a commit fa50ff7

2 files changed

Lines changed: 25 additions & 26 deletions

File tree

src/main/java/com/bitso/examples/BitsoJavaExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import javax.crypto.Mac;
88
import javax.crypto.spec.SecretKeySpec;
99

10-
import org.apache.http.client.methods.CloseableHttpResponse;
11-
import org.apache.http.client.methods.HttpPost;
12-
import org.apache.http.entity.StringEntity;
13-
import org.apache.http.impl.client.HttpClients;
10+
import org.apache.hc.client5.http.classic.methods.HttpPost;
11+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
12+
import org.apache.hc.client5.http.impl.classic.HttpClients;
13+
import org.apache.hc.core5.http.io.entity.StringEntity;
1414
import org.json.JSONObject;
1515

1616
public class BitsoJavaExample {

src/main/java/com/bitso/http/BlockingHttpClient.java

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77
import java.net.URL;
88
import java.nio.charset.Charset;
99
import java.util.HashMap;
10+
import java.util.Map;
1011
import java.util.Map.Entry;
1112

1213
import 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-
2415
import com.bitso.exceptions.BitsoAPIException;
2516

2617
import 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

2829
public 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

Comments
 (0)