Android Spring RestTemplate with proxy

I need to read the proxy information from the phone, and if the phone is under the proxy server, I need to install this proxy server on my spring restTemplate object. I tried the following code with no luck, could someone tell me what I'm doing wrong.

String proxyHost = android.net.Proxy.getHost(activity .getApplicationContext()); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, 8080)); requestFactory.setProxy(proxy); RestTemplate rest = new RestTemplate(requestFactory); 

Thanks for any help

+4
source share
1 answer

I found the answer to my question .. will output the code here:

  if (proxyHost != null && proxyPort != null) { rest = new RestTemplate(); HttpComponentsClientHttpRequestFactory factory = ((HttpComponentsClientHttpRequestFactory) rest .getRequestFactory()); DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory .getHttpClient(); HttpHost proxy = new HttpHost(proxyHost, proxyPort); defaultHttpClient.getParams().setParameter( ConnRoutePNames.DEFAULT_PROXY, proxy); } else { rest = new RestTemplate(); HttpComponentsClientHttpRequestFactory factory = ((HttpComponentsClientHttpRequestFactory) rest .getRequestFactory()); DefaultHttpClient defaultHttpClient = (DefaultHttpClient) factory .getHttpClient(); } 
+5
source

Source: https://habr.com/ru/post/1380458/


All Articles