You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
951 B

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. public class HttpApi {
  7. public static String sendHttpGETRequest(String url) throws IOException {
  8. URL obj = new URL(url);
  9. HttpURLConnection httpURLConnection = (HttpURLConnection) obj.openConnection();
  10. httpURLConnection.setRequestMethod("GET");
  11. int responseCode = httpURLConnection.getResponseCode();
  12. if (responseCode == HttpURLConnection.HTTP_OK) {
  13. BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
  14. String inputLine;
  15. StringBuffer response = new StringBuffer();
  16. while ((inputLine = in .readLine()) != null) {
  17. response.append(inputLine);
  18. } in .close();
  19. return response.toString();
  20. }
  21. return null;
  22. }
  23. }