Skip to content

Commit 43aec32

Browse files
author
Eugen
committed
Merge pull request eugenp#145 from colemarkham/master
Simplify to use HEAD instead of GET requests
2 parents 9fedd67 + ed0165c commit 43aec32

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

httpclient/src/test/java/org/baeldung/httpclient/rare/HttpClientUnshortenLiveTest.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.apache.http.HttpEntity;
1414
import org.apache.http.HttpHeaders;
1515
import org.apache.http.HttpResponse;
16-
import org.apache.http.client.methods.HttpGet;
16+
import org.apache.http.client.methods.HttpHead;
1717
import org.apache.http.impl.client.CloseableHttpClient;
1818
import org.apache.http.impl.client.HttpClientBuilder;
1919
import org.apache.http.util.EntityUtils;
@@ -82,12 +82,12 @@ final String expandSafe(final String urlArg) throws IOException {
8282
}
8383

8484
final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOException {
85-
HttpGet request = null;
85+
HttpHead request = null;
8686
HttpEntity httpEntity = null;
8787
InputStream entityContentStream = null;
8888

8989
try {
90-
request = new HttpGet(url);
90+
request = new HttpHead(url);
9191
final HttpResponse httpResponse = client.execute(request);
9292

9393
httpEntity = httpResponse.getEntity();
@@ -118,17 +118,12 @@ final Pair<Integer, String> expandSingleLevelSafe(final String url) throws IOExc
118118
}
119119

120120
final String expandSingleLevel(final String url) throws IOException {
121-
HttpGet request = null;
122-
HttpEntity httpEntity = null;
123-
InputStream entityContentStream = null;
121+
HttpHead request = null;
124122

125123
try {
126-
request = new HttpGet(url);
124+
request = new HttpHead(url);
127125
final HttpResponse httpResponse = client.execute(request);
128126

129-
httpEntity = httpResponse.getEntity();
130-
entityContentStream = httpEntity.getContent();
131-
132127
final int statusCode = httpResponse.getStatusLine().getStatusCode();
133128
if (statusCode != 301 && statusCode != 302) {
134129
return url;
@@ -144,12 +139,6 @@ final String expandSingleLevel(final String url) throws IOException {
144139
if (request != null) {
145140
request.releaseConnection();
146141
}
147-
if (entityContentStream != null) {
148-
entityContentStream.close();
149-
}
150-
if (httpEntity != null) {
151-
EntityUtils.consume(httpEntity);
152-
}
153142
}
154143
}
155144

0 commit comments

Comments
 (0)