RestTemplate without proxy works as expected, but after set a proxy, it only got the first string of the response.
package pub.itpark.health.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpHost;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author LDC
*/
@Slf4j
@SpringBootTest
public class RestTemplateTest {
final String url = "https://ipinfo.io/json";
@Value("${config.spider.proxy.host}")
private String proxyHost;
@Value("${config.spider.proxy.port}")
private int proxyPort;
@Value("${config.spider.proxy.username}")
private String proxyUsername;
@Value("${config.spider.proxy.password}")
private String proxyPassword;
@Test
void restTemplateWithProxy() {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(proxyUsername, proxyPassword.toCharArray())
);
HttpClient httpClient = HttpClients.custom()
.setProxy(proxy)
.setDefaultCredentialsProvider(credentialsProvider)
.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);
String result = restTemplate.getForObject(url, String.class);
log.info("RestTemplate with proxy get result: {}", result);
assertNotNull(result);
}
@Test
void restTemplateWithoutProxy() {
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(url, String.class);
log.info("RestTemplate without proxy get result: {}", result);
assertNotNull(result);
}
}
Here is the log result:
2025-03-14T11:24:29.058+08:00 INFO 5700 --- [ Test worker] [smart-health-backend,,]p.itpark.health.util.RestTemplateTest : RestTemplate without proxy get result: {
"ip": "183.223.249.124",
"city": "Chengdu",
"region": "Sichuan",
"country": "CN",
"loc": "30.6667,104.0667",
"org": "AS9808 China Mobile Communications Group Co., Ltd.",
"postal": "610000",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}
2025-03-14T11:24:33.488+08:00 INFO 5700 --- [ Test worker] [smart-health-backend,,]p.itpark.health.util.RestTemplateTest : RestTemplate with proxy get result: {
Comment From: bclozel
It doesn't look like a Spring issue but rather a proxy problem or an HttpComponent problem.
Can you try to do the same with curl and share the HTTP response headers and response in both cases ?
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.