Advertisement
dereksir

Untitled

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