1
wangsongyan 2021-01-13 09:28:06 +08:00
短信发出去乱码?仔细看下短信接口文档
|
2
wangyanrui 2021-01-13 09:32:46 +08:00
看一下 StringHttpMessageConverter 源码
记得默认的编码是 IOS 8859-1 |
3
springmarker 2021-01-13 09:34:36 +08:00
get 方法发短信?
|
4
xuwei0056 2021-01-13 09:42:02 +08:00
先 urlEncode ?
|
5
90d0n 2021-01-13 09:46:11 +08:00
```
List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters(); for (HttpMessageConverter<?> httpMessageConverter : list) { if ( httpMessageConverter instanceof StringHttpMessageConverter) { ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(StandardCharsets.UTF_8); break; } } ``` 替换一下 converter 试试? |
6
sandman511 OP @xuwei0056 这个试过了 还是不行
|
7
chendy 2021-01-13 10:35:49 +08:00
拼接在 url 上的中文要 urlEncode
用 UrlEncoder.encode(msg, "GBK") |
8
sandman511 OP |
9
sandman511 OP @xuwei0056
@chendy 使用 HttpClient PostMethod 配合 URLEncoder 是可以成功发送中文的: HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager(); HttpConnectionManagerParams params = httpConnectionManager.getParams(); params.setConnectionTimeout(5000); params.setSoTimeout(20000); params.setDefaultMaxConnectionsPerHost(1000); params.setMaxTotalConnections(1000); client = new HttpClient( httpConnectionManager); PostMethod post = new PostMethod(URL + URLEncoder.encode(alarmMsgInfo, "GBK")); client.executeMethod(post); 但是用 restTemplate 就不行 发的是 %C4%E3%BA%C3 |
10
chendy 2021-01-13 10:54:42 +08:00 1
@sandman511 忘记了,直接放 url 字符串会多一次 urlEnocded,暴力方法是下面这样自己 encode url (优雅方法是自定以 UriHandler,有点麻烦……):
RestTemplate rt = new RestTemplateBuilder().build(); UriComponentsBuilder uri = UriComponentsBuilder.fromUriString("http://localhost:8080/test"); uri.queryParam("x", "中文"); uri.encode(Charset.forName("GBK")); System.out.println(uri.toUriString()); Map s = rt.getForObject(uri.build().toUri(), Map.class); System.out.println(s); |
11
sandman511 OP @chendy 可以了!感谢帮助!
|