62

I have a web service running on my dev box implemented using Spring-MVC 3.0. I have various JUnits that test against that service using RestTemplate. What I would like to do is have JMeter pick up those JUnits REST requests when I run them. However, to do that, I need to have Spring's RestTemplate send them to the proxy that I'm running JMeter on. So, the question is, how can I do that?

I've done something similar with CXF and their http:conduit and http:client stuff, but I really have no idea how to do this with Spring-MVC.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109

5 Answers5

115

@AHungerArtist's answer works for simple use cases, where you want all requests to use the same proxy. If you need some requests through restTemplate to use the proxy, and others to not, though, you may find this more useful. (Or if you just like doing it programmatically more than you like mucking with system properties!)

@Bean
public RestTemplate restTemplate() {
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();

    Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("my.host.com", 8080));
    requestFactory.setProxy(proxy);

    return new RestTemplate(requestFactory);
}

You should be able to create a copy of the restTemplate bean that way, and another one the normal way, so you can send requests with and without the proxy.

CorayThan
  • 17,174
  • 28
  • 113
  • 161
  • 25
    Just as a note for anyone else looking at this, the full reference is java.net.Proxy for the Proxy class. – Paul Mar 23 '16 at 20:09
  • I actually prefer this way. For example if I have multiple services using diferent hosts I can configure per target host if I want to go over proxy or not. – Jorge Machado Aug 03 '17 at 11:27
  • Does this proxy handles well POST with multipart content type requests? – NiVeR Mar 06 '18 at 19:22
  • which proxy class to use? `java.net.Proxy` or `org.springframework.cglib.proxy.Proxy` ? – Suresh Jul 05 '18 at 03:47
  • 3
    Same for the Type class -> java.net.Proxy.Type – socona Nov 16 '18 at 09:26
  • For friends, who want send requests via TOR Socks. If Y have 501 response - try change proxy type to SOCKS: [code]Proxy prx = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("localhost", 9050));[code] – Mikro Koder May 24 '19 at 10:29
19

Sadly, this was really easy.


Properties props = System.getProperties();
props.put("http.proxyHost", "localhost");
props.put("http.proxyPort", "9080");
AHungerArtist
  • 9,332
  • 17
  • 73
  • 109
  • Ah. That's covered on the Charles' documentation. http://www.charlesproxy.com/documentation/using-charles/ssl-certificates/ – yincrash Jun 28 '11 at 18:11
  • 10
    Please do remember, these method has a side effect, since those codes are setup your jvm to use a proxy. If you have another application that run under the same jvm, and that application doesn't need to use proxy, it will be forced to use the same proxy you setup before. – the.wizard Nov 24 '15 at 01:42
  • 1
    Just want to point out that if you are sending HTTP**S** requests, you need to set properties `https.proxyHost` and `https.proxyPort`. And if you are using a SOCKS proxy, there are the properties `socksProxyHost` and `socksProxyPort` (without the dots because of "historical reasons"). [Source](https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html) – Gui Meira Sep 29 '20 at 02:45
9

Spring has a good documentation using a Customizer to determine different proxy

public class ProxyCustomizer implements RestTemplateCustomizer {

    @Override
    public void customize(RestTemplate restTemplate) {
        final String proxyUrl = "proxy.example.com";
        final int port = 3128;

        HttpHost proxy = new HttpHost(proxyUrl, port);
        HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
            @Override
            protected HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context)
                    throws HttpException {
                if (target.getHostName().equals("gturnquist-quoters.cfapps.io")) {
                    return super.determineProxy(target, request, context);
                }
                return null;
            }
        }).build();
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));

    }

}

and the call to apply the ProxyCustomizer is

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.additionalCustomizers(new ProxyCustomizer()).build();
}
Bahadır Yağan
  • 5,577
  • 3
  • 35
  • 39
Nicolas
  • 186
  • 2
  • 10
2

Alternatively you can use runtime parameters:

jre -DproxySet=true -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888
itstata
  • 1,058
  • 7
  • 17
1

put these lines before calling your get or post method. so proxy get set .

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    DefaultHttpClient httpClient = (DefaultHttpClient) requestFactory.getHttpClient();
    HttpHost proxy = new HttpHost("proxtserver", port);
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
    restTemplate.setRequestFactory(requestFactory);
abhishek ringsia
  • 1,970
  • 2
  • 20
  • 28