Last active
December 16, 2015 11:49
-
-
Save hschmitt/5430248 to your computer and use it in GitHub Desktop.
Helper downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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