Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
rest template with utf-8 text/html
package com.google.locationlab;
import com.google.common.collect.Lists;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
import org.springframework.http.*;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.List;
public class PortalRestTemplateIntegrationTest {
private Log logger = LogFactory.getLog(PortalRestTemplateIntegrationTest.class);
@Test
public void test() throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(Charset.forName("UTF-8"));
List<HttpMessageConverter<?>> httpMessageConverter = Lists.newArrayList();
httpMessageConverter.add(stringHttpMessageConverter);
restTemplate.setMessageConverters(httpMessageConverter);
URI targetUrl= UriComponentsBuilder.fromUriString("http://portal.net")
.path("search")
.queryParam("q", "잠실역")
.build()
.toUri();
HttpHeaders headers = new HttpHeaders();
Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType("text", "html", utf8);
headers.setContentType(mediaType);
headers.set("User-Agent", "mozilla");
headers.set("Accept-Language", "ko");
// gzip 사용하면 byte[] 로 받아서, 압축을 풀고 decoding 해야 한다.
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(targetUrl.toURL().toString(), HttpMethod.GET, entity, String.class);
String result = responseEntity.getBody();
logger.info(result);
}
}
@arioss
Copy link

arioss commented Sep 5, 2020

Solved my problem. Thx!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment