Skip to content

Instantly share code, notes, and snippets.

@zhaoyou
Last active March 9, 2017 15:06
Show Gist options
  • Save zhaoyou/03f374d3449d4d3ed0dcca85b6407635 to your computer and use it in GitHub Desktop.
Save zhaoyou/03f374d3449d4d3ed0dcca85b6407635 to your computer and use it in GitHub Desktop.
根据URL获取HTTP Response的整个结果字符串
/*
** 不同的方式获取结果 [InputStream -> String] : http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string
*/
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment