Skip to content

Instantly share code, notes, and snippets.

@benelog
Created August 30, 2012 05:48
Show Gist options
  • Save benelog/3522924 to your computer and use it in GitHub Desktop.
Save benelog/3522924 to your computer and use it in GitHub Desktop.
Http client
DefaultHttpClient client = new DefaultHttpClient();
// HttpClient를 매번생성하지 않고 ThreadSafeClientConnManager 를 활용하는 것이 바람직
HttpPost request = new HttpPost(apiUrl);
UrlEncodedFormEntity formEntity = createEntity(userId, password);
request.setEntity(formEntity);
InputStream contentStream = null;
String resContent = null;
try {
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
contentStream = resEntity.getContent();
resContent = IoUtils.readFully(contentStream);
} catch (IOException e) {
throw new IllegalStateException("fail to connect api server :" + apiUrl ,e);
} finally {
IoUtils.close(contentStream);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment