我使用下面的代码将基本http身份验证的凭据设置为使用Spring Security的服务器。 不幸的是我有像é,ò等特殊字符的问题...我在服务器上收到问号而不是正确的字符 有人知道如何解决它?我一直在寻找设置编码没有成功使用Spring身份验证的特殊字符
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class RestClient extends RestTemplate {
public RestClient(String username, String password) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
}
然后我调用Web服务与弹簧类:
RestClient restClient = new RestClient(username, password);
response = restClient.getForObject(addQueryParam(url, queryParams), Response.class);
UPDATE:
使用此代码我有同样的错误(问号而不是特殊字符)。一些想法?
public class RestClient extends RestTemplate {
private static RestClient instance;
private RestClient(String username, String password) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
public static synchronized RestClient getInstance(String username, String password){
if (instance == null){
instance = new RestClient(username, password);
instance.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8")));
}
return instance;
}
}
“不幸的是我有特殊字符,如E,O等问题。”好的,问题到底是什么? – rmlan
我收到服务器问号而不是正确字符 – luca