Advertisement
dereksir

Untitled

Apr 29th, 2024 (edited)
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package com.example;
  2.  
  3. // import the required classes
  4. import okhttp3.OkHttpClient;
  5. import okhttp3.Request;
  6. import okhttp3.Response;
  7.  
  8. import java.io.IOException;
  9. import java.net.InetSocketAddress;
  10. import java.net.Proxy;
  11.  
  12. public class Main {
  13.     String run(String url) throws IOException {
  14.         // define your proxy details
  15.         String proxyHost = "140.238.247.9";
  16.         int proxyPort = 8100;
  17.  
  18.         // create a proxy object and pass in the necessary details
  19.         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
  20.  
  21.         // create a OkHttpClient builder instance and configure it to use the proxy
  22.         OkHttpClient client = new OkHttpClient.Builder()
  23.             .proxy(proxy)
  24.             .build();
  25.  
  26.         // create a request with the provided URL
  27.         Request request = new Request.Builder()
  28.             .url(url)
  29.             .build();
  30.         // execute the request and obtain the response
  31.         try (Response response = client.newCall(request).execute()) {
  32.             // return the response body as a string
  33.             return response.body().string();
  34.         }
  35.     }
  36.  
  37.     public static void main(String[] args) throws IOException {
  38.         // create an instance of the Main class
  39.         Main example = new Main();
  40.         // make a GET request to the specified URL and print the response
  41.         String response = example.run("https://httpbin.io/ip");
  42.         System.out.println(response);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement