OkHttp proxy settings

I need to configure a proxy to send JSON using POST using proxyHost and proxyPort.

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); Proxy proxyTest = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy", proxyPort)); OkHttpClient client = new OkHttpClient() .proxy(proxyTest) .build(); //OkHttpClient.Builder builder = new OkHttpClient.Builder(); //builder.proxy(proxySAP); //client.setProxy(proxySAP) //OkHttpClient client = builder.build();; String post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } 

When I try to use the proxy test, which I saw in some answers here, it indicates an error:

The proxy () method in type OkHttpClient is not applicable for arguments (proxies)

Iam using OKHTTP 3.3.1 (okhttp3)

My question is: what should I do? I have done several such tests:

OkHttpClient.Builder builder = new OkHttpClient.Builder ();
builder.proxy (proxyTest);
client.setProxy (proxyTest)
OkHttpClient client = builder.build ();

But so far nothing is working.

Thank you for your time!

+8
java json post proxy
source share
1 answer

Found a solution:

  //OkHttpClient client = new OkHttpClient(); OkHttpClient.Builder builder = new OkHttpClient.Builder().proxy(proxyTest); OkHttpClient client = builder.build(); //builder.proxy(proxyTest); //client.setProxy(proxyTest) //OkHttpClient client = builder.build();; 

If we use the builder to enter the proxy server, it will work like charm = D

Best wishes!

+8
source

All Articles