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 us...



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 

 

相关文章
|
JSON JavaScript 数据格式
proxy error: could not proxy request解决方案
proxy error: could not proxy request解决方案
10031 5
proxy error: could not proxy request解决方案
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
POST 请求出现异常!java.io.IOException: Server returned HTTP response code: 400 for URL
1153 0
|
16天前
|
JSON 编解码 安全
【HTTP】方法(method)以及 GET 和 POST 的区别
【HTTP】方法(method)以及 GET 和 POST 的区别
52 1
|
数据安全/隐私保护 Python
解决CondaHTTPError:HTTP 000 CONNECTION FAILED for url<https://mirrors.tuna.tsinghua.edu.cn/anaconda***
今天做项目的时候,Python导入一个包一直有各类问题,而后最终锁定问题是CondaHTTPError:HTTP 000 CONNECTION FAILED for url<https://mirrors.tuna.tsinghua.edu.cn/anaconda*** 这就是清华的源出问题了,配置没配对。
解决CondaHTTPError:HTTP 000 CONNECTION FAILED for url<https://mirrors.tuna.tsinghua.edu.cn/anaconda***
Proxy error: Could not proxy request
Proxy error: Could not proxy request
2408 0
Proxy error: Could not proxy request
HTTP request以及response原理 request请求消息数据
HTTP request以及response原理 request请求消息数据
|
11月前
|
JavaScript
Proxy error: Could not proxy request错误解决
Proxy error: Could not proxy request错误解决
483 0
|
Web App开发 JSON 数据格式
使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
使用 http-proxy 代理 HTTP 请求时遇到的 the requested url is invalid 错误消息
|
安全 网络协议 Ubuntu
解决CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/
解决CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/
2389 0
|
Web App开发
JavaWeb - HTTP 请求中 Post 和 Put 区别
JavaWeb - HTTP 请求中 Post 和 Put 区别
214 0