BAEL-3844 : Send request to a proxy using RestTemplate
This commit is contained in:
parent
4f12335bcf
commit
020aff1160
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.resttemplate.proxy;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.Proxy.Type;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class RequestFactoryLiveTest {
|
||||
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("201.91.82.155", 3128));
|
||||
|
||||
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
||||
requestFactory.setProxy(proxy);
|
||||
|
||||
restTemplate = new RestTemplate(requestFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() {
|
||||
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
|
||||
|
||||
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.resttemplate.proxy;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.apache.http.HttpException;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.HttpRequest;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
|
||||
import org.apache.http.protocol.HttpContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.web.client.RestTemplateBuilder;
|
||||
import org.springframework.boot.web.client.RestTemplateCustomizer;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class RestTemplateCustomizerLiveTest {
|
||||
|
||||
RestTemplate restTemplate;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
restTemplate = new RestTemplateBuilder(new ProxyCustomizer()).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestTemplate_whenRequestedWithProxy_thenResponseBodyIsOk() {
|
||||
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
|
||||
|
||||
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
|
||||
}
|
||||
|
||||
private static class ProxyCustomizer implements RestTemplateCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(RestTemplate restTemplate) {
|
||||
HttpHost proxy = new HttpHost("201.91.82.155", 3128);
|
||||
HttpClient httpClient = HttpClientBuilder.create()
|
||||
.setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
|
||||
@Override
|
||||
public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
|
||||
return super.determineProxy(target, request, context);
|
||||
}
|
||||
})
|
||||
.build();
|
||||
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue