zoukankan      html  css  js  c++  java
  • Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?



    HttpComponentsClientHttpRequestFactory

    If you are using Spring MVC's RestTemplate to make REST calls, it is important to realize that it doesn't use HTTP connection pooling of any kind, and will establish and close a connection every time you make a REST call.

    If you want to use connection pooling, you would need to provide another implementation of ClientHttpRequestFactory. A good option is to use the org.springframework.http.client.HttpComponentsClientHttpRequestFactory() which is provided with Spring.

    new org.springframework.web.client.RestTemplate(new HttpComponentsClientHttpRequestFactory())
    Of course, if you look up the documentation for HttpComponentsClientHttpRequestFactory, you can configure a lot of the connection pooling parameters.

    https://coderwall.com/p/dcohra/connection-pooling-with-spring-resttemplate

    http://stackoverflow.com/questions/25698072/simpleclienthttprequestfactory-vs-httpcomponentsclienthttprequestfactory-for-htt

    org.springframework.http.client.SimpleClientHttpRequestFactory

    java.net.Proxy

    java.net.Proxy.Type

    java.net.InetSocketAddress

    org.springframework.web.client.RestTemplate

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

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

    http://stackoverflow.com/questions/3687670/using-resttemplate-how-to-send-the-request-to-a-proxy-first-so-i-can-use-my-jun 

  • 相关阅读:
    18、【opencv入门】形态学图像处理(一):开运算、闭运算、形态学梯度、顶帽、黑帽合辑
    17、【opencv入门】形态学图像处理(一):膨胀与腐蚀
    16、【opencv入门】创建Trackbar & 图像对比度、亮度值调整
    c++ 售货员的难题
    c++ 火柴棒等式
    c++ 素数圈
    c++ 分解数
    c++ 走迷宫
    c++ 二叉树遍历
    c++ n皇后问题
  • 原文地址:https://www.cnblogs.com/softidea/p/6211131.html
Copyright © 2011-2022 走看看