Advertisement
dereksir

Untitled

Apr 29th, 2024 (edited)
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package com.example;
  2.  
  3. // import the necessary classes
  4. import java.io.IOException;
  5. import java.net.InetSocketAddress;
  6. import java.net.Proxy;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Random;
  10.  
  11. import okhttp3.*;
  12.  
  13. public class Main {
  14.         // define a proxy pool
  15.        private static final List<ProxyInfo> proxyList = new ArrayList<>();
  16.  
  17.     static {
  18.         proxyList.add(new ProxyInfo("140.238.247.9", 8100));
  19.         proxyList.add(new ProxyInfo("213.188.211.61", 3128));
  20.         proxyList.add(new ProxyInfo("67.43.227.229", 20195));
  21.     }
  22.  
  23.     // create static proxyInfo class
  24.     static class ProxyInfo {
  25.         String host;
  26.         int port;
  27.  
  28.         ProxyInfo(String host, int port) {
  29.             this.host = host;
  30.             this.port = port;
  31.         }
  32.     }
  33.  
  34.     String run(String url) throws IOException {
  35.         // randomly select a proxy from the list
  36.         Random random = new Random();
  37.         int index = random.nextInt(proxyList.size());
  38.         ProxyInfo proxyInfo = proxyList.get(index);
  39.  
  40.         // create a proxy object with the selected proxy details
  41.         Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyInfo.host, proxyInfo.port));
  42.        
  43.         // create a OkHttpClient builder instance and configure it to use the proxy
  44.         OkHttpClient client = new OkHttpClient.Builder()
  45.             .proxy(proxy)
  46.             .build();
  47.  
  48.         // create a request with the provided URL
  49.         Request request = new Request.Builder()
  50.             .url(url)
  51.             .build();
  52.         // execute the request and obtain the response
  53.         try (Response response = client.newCall(request).execute()) {
  54.             // return the response body as a string
  55.             return response.body().string();
  56.         }
  57.     }
  58.  
  59.     public static void main(String[] args) throws IOException {
  60.         // create an instance of the Main class
  61.         Main example = new Main();
  62.         // make a GET request to the specified URL and print the response
  63.         String response = example.run("https://httpbin.io/ip");
  64.         System.out.println(response);
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement