Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example;
- // import the necessary classes
- import java.io.IOException;
- import java.net.InetSocketAddress;
- import java.net.Proxy;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import okhttp3.*;
- public class Main {
- // define a proxy pool
- private static final List<ProxyInfo> proxyList = new ArrayList<>();
- static {
- proxyList.add(new ProxyInfo("140.238.247.9", 8100));
- proxyList.add(new ProxyInfo("213.188.211.61", 3128));
- proxyList.add(new ProxyInfo("67.43.227.229", 20195));
- }
- // create static proxyInfo class
- static class ProxyInfo {
- String host;
- int port;
- ProxyInfo(String host, int port) {
- this.host = host;
- this.port = port;
- }
- }
- String run(String url) throws IOException {
- // randomly select a proxy from the list
- Random random = new Random();
- int index = random.nextInt(proxyList.size());
- ProxyInfo proxyInfo = proxyList.get(index);
- // create a proxy object with the selected proxy details
- Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyInfo.host, proxyInfo.port));
- // create a OkHttpClient builder instance and configure it to use the proxy
- OkHttpClient client = new OkHttpClient.Builder()
- .proxy(proxy)
- .build();
- // create a request with the provided URL
- Request request = new Request.Builder()
- .url(url)
- .build();
- // execute the request and obtain the response
- try (Response response = client.newCall(request).execute()) {
- // return the response body as a string
- return response.body().string();
- }
- }
- public static void main(String[] args) throws IOException {
- // create an instance of the Main class
- Main example = new Main();
- // make a GET request to the specified URL and print the response
- String response = example.run("https://httpbin.io/ip");
- System.out.println(response);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement