如何为我的 android spring resttemplate 使用 SSL
How to use SSL for my android spring resttemplate
我一直在使用 Spring 的 RestTemplate 来访问我的网络服务,但现在我需要使用 ssl。我一直在搜索并找到了一些示例,但 none 有效(我有很多关于已弃用函数的问题,我无法弄清楚)
这是我到目前为止的使用方式
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());
}
Gradle
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?
我最后扔掉了 Spring。我无法使用 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;
}
我一直在使用 Spring 的 RestTemplate 来访问我的网络服务,但现在我需要使用 ssl。我一直在搜索并找到了一些示例,但 none 有效(我有很多关于已弃用函数的问题,我无法弄清楚)
这是我到目前为止的使用方式
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());
}
Gradle
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?
我最后扔掉了 Spring。我无法使用 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;
}