Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WebClient usage
String response = null;
HttpClient httpclient = null;
try {
HttpGet httpget = new HttpGet(url.toURI());
httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httpget);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new Exception("Got HTTP " + statusCode
+ " (" + httpResponse.getStatusLine().getReasonPhrase() + ')');
}
response = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpclient != null) {
httpclient.getConnectionManager().shutdown();
httpclient = null;
}
}
return response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment