Skip to content

Instantly share code, notes, and snippets.

@hschmitt
Last active December 16, 2015 11:49
Show Gist options
  • Save hschmitt/5430248 to your computer and use it in GitHub Desktop.
Save hschmitt/5430248 to your computer and use it in GitHub Desktop.
Helper downloader
public String getStringFromURL(String endpoint) {
HttpClient httpclient = new DefaultHttpClient();
CredentialsProvider credProvider = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
new UsernamePasswordCredentials("YOUR USER NAME HERE", "YOUR PASSWORD HERE"));
httpclient.setCredentialsProvider(credProvider);
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpGet httpget = new HttpGet(endpoint);
StringBuilder sb = new StringBuilder("");
HttpResponse response;
InputStream is;
try {
response = httpclient.execute(httpget);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment