TabProxy
  • Tutorials
  • Rotating Residential Proxy
    • User & Pass Auth
    • API extraction
  • Unlimited Residential Proxy
    • User& Pass Auth
    • API extraction
  • Static Residential Proxy
    • User & Pass Auth
    • API extraction
  • Dedicated Datacenter Proxy
    • User & Pass Auth
    • API extraction
  • Common Problems
    • IP Whitelist Configuration
    • Enterprise CDK Exchange
    • Sub-user configuration
    • Google extension configuration tutorial
    • pop-up payment
  • Code Examples
    • C/C++
    • GO
    • Node.js
    • PHP
    • JAVA
    • Python
  • Integration Tutorial
Powered by GitBook
On this page
  1. Code Examples

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);

        }

    }

      
PreviousPHPNextPython

Last updated 1 year ago