Permalink
Browse files

Use default HTTP client

I experienced that this shared pooling client won't send further
requests, if not closed. Closing is not practical when working with the
response stream.
  • Loading branch information...
malkusch committed Mar 19, 2016
1 parent 5d06fa9 commit f94164cec14fd5f343e33826fcca19c5c27bfcb5
Showing with 3 additions and 18 deletions.
  1. +3 −18 src/main/java/de/malkusch/whoisApi/WhoisApi.java
@@ -1,6 +1,5 @@
package de.malkusch.whoisApi;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
@@ -16,9 +15,7 @@
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -48,14 +45,12 @@
* @see <a href="http://whois-api.domaininformation.de/">Whois API</a>
*/
@ThreadSafe
public class WhoisApi implements Closeable {
public class WhoisApi {
private final String apiKey;
private final URI baseUri;
private final CloseableHttpClient client;
private static final String DEFAULT_BASE_URI = "https://whois-v0.p.mashape.com/";
private static final String API_KEY_HEADER = "X-Mashape-Key";
@@ -92,13 +87,8 @@ public WhoisApi(final String apiKey, final URI baseUri) {
this.apiKey = apiKey;
this.baseUri = baseUri;
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(100);
cm.setMaxTotal(200);
client = HttpClients.custom().setConnectionManager(cm).build();
}
/**
* Checks if a domain is available and returns the whois response.
*
@@ -238,7 +228,7 @@ private InputStream sendRequest(URI uri) throws RecoverableWhoisApiException {
HttpGet get = new HttpGet(uri);
get.addHeader(API_KEY_HEADER, apiKey);
HttpResponse response = client.execute(get);
HttpResponse response = HttpClients.createDefault().execute(get);
StatusLine statusLine = response.getStatusLine();
switch (statusLine.getStatusCode()) {
@@ -266,9 +256,4 @@ private InputStream sendRequest(URI uri) throws RecoverableWhoisApiException {
}
}
@Override
public void close() throws IOException {
client.close();
}
}

0 comments on commit f94164c

Please sign in to comment.