我一直在使用Spring的RestTemplate访问我的网络服务,但现在我需要使用ssl。我一直在寻找,并找到了一些例子,但没有一个有效(我有很多废弃的功能,我无法弄清楚)
到目前为止我是如何使用它的
RestTemplate restTemplate = new RestTemplate();
// Add the String message converter
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
// Make the HTTP GET request, marshaling the response to a String
String result = restTemplate.getForObject(urlQuery, String.class, "GetUnit/" + tM.getDeviceId());
}
摇篮
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'org.springframework.android:spring-android-rest-template:2.0.0.M3'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.1'
}
我如何做这个做ssl?
答案 0 :(得分:0)
我最终扔掉了春天。我无法使用Spring找到解决方案。
所以现在我依靠HttpsUrlConnection并使用gson发送对象
public String GetUnit(String url) {
String result = null;
HttpURLConnection urlConnection = null;
try {
URL requestedUrl = new URL(url);
urlConnection = (HttpURLConnection) requestedUrl.openConnection();
if (urlConnection instanceof HttpsURLConnection) {
((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslContext.getSocketFactory());
((HttpsURLConnection) urlConnection).setHostnameVerifier(new BrowserCompatHostnameVerifier());
}
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(1500);
urlConnection.setReadTimeout(1500);
lastResponseCode = urlConnection.getResponseCode();
result = IOUtil.readFully(urlConnection.getInputStream());
} catch (Exception ex) {
result = ex.toString();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return result;
}