Skip to content

Instantly share code, notes, and snippets.

@fsschmitt
Created June 10, 2012 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsschmitt/2906703 to your computer and use it in GitHub Desktop.
Save fsschmitt/2906703 to your computer and use it in GitHub Desktop.
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