Advertisement
dereksir

Untitled

Apr 29th, 2024
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. package com.example;
  2.  
  3. // import the required classes
  4. import java.io.IOException;
  5. import okhttp3.OkHttpClient;
  6. import okhttp3.Request;
  7. import okhttp3.Response;
  8.  
  9. public class Main {
  10.     // create a new OkHttpClient instance
  11.     final OkHttpClient client = new OkHttpClient();
  12.  
  13.     String run(String url) throws IOException {
  14.         // create a request with the provided URL
  15.         Request request = new Request.Builder()
  16.             .url(url)
  17.             .build();
  18.         // execute the request and obtain the response
  19.         try (Response response = client.newCall(request).execute()) {
  20.             // return the response body as a string
  21.             return response.body().string();
  22.         }
  23.     }
  24.  
  25.     public static void main(String[] args) throws IOException {
  26.         // create an instance of the Main class
  27.         Main example = new Main();
  28.         // make a GET request to the specified URL and print the response
  29.         String response = example.run("https://httpbin.io/ip");
  30.         System.out.println(response);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement