Skip to content

Instantly share code, notes, and snippets.

@kwabe007
Created October 23, 2019 20:00
Show Gist options
  • Save kwabe007/504ebe045ae700b36148d23bef06c88d to your computer and use it in GitHub Desktop.
Save kwabe007/504ebe045ae700b36148d23bef06c88d to your computer and use it in GitHub Desktop.
Sample code for making a HTTP request in java
public static String makeHTTPRequestForLavenderTown() {
String urlString = "https://pokeapi.co/api/v2/location/lavender-town";
String response = null;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
Scanner responseScanner = new Scanner(connection.getInputStream());
while (responseScanner.hasNext()) {
response = responseScanner.nextLine();
}
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment