< Back to blog

Code Examples

Mastering JAVA: Unlocking the Secrets of Exceptional Code

blog
2024-04-02

JAVA

Example


package demo;



    import okhttp3.OkHttpClient;

    import okhttp3.Request;



    import java.io.IOException;

    import java.net.InetSocketAddress;

    import java.net.Proxy;



    /**

    * compile 'com.squareup.okhttp3:okhttp:3.10.0'

    */

    class ApiProxyJava {

        public static void main(String[] args) throws IOException {

  testHttpWithOkHttp();

  testSocks5WithOkHttp();

        }



        public static void testHttpWithOkHttp() throws IOException {

  String url = "http://api.myip.la/en?json";

  Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("ip", 2000));

  OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build();



  Request request = new Request.Builder().url(url).build();

  okhttp3.Response response = client.newCall(request).execute();

  String responseString = response.body().string();

  System.out.println(responseString);

        }



        public static void testSocks5WithOkHttp() throws IOException {

  String url = "http://api.myip.la/en?json";

  Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("ip", 8080));

  OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build();



  Request request = new Request.Builder().url(url).build();

  okhttp3.Response response = client.newCall(request).execute();

  String responseString = response.body().string();

  System.out.println(responseString);

        }

    }

      



1
Mastering JAVA: Unlocking the Secrets of Exceptional Code

Forget about complex web scraping processesChoose

Tabproxy advanced web intelligence collectiosolutions to gather real-time public data hassle-free

Sign Up

Related articles

Mastering JAVA: Unlocking the Secrets of Exceptional Code
2024-04-02
Exploring the Power of PHP: A Dive into the World of Code
2024-04-02
How to Harness the Power of Node.js: A Complete Guide to Coding
2024-04-02