我尝试在我的Android应用中执行HTTPS POST请求。首先,我尝试使用通用的okHttp客户端发出请求,并收到SSLHandshakeException。为了防止这种情况,我开始使用带有以下代码的自签名证书:
public class SelfSignedTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
public static void setUnsafeHttpsSettings(OkHttpClient.Builder builder) {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new SelfSignedTrustManager[] {
new SelfSignedTrustManager()
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
}
class ApiClass {
private val httpClient: OkHttpClient
constructor() {
val builder = OkHttpClient().newBuilder()
Utils.setUnsafeHttpsSettings(builder)
httpClient = builder.build()
}
fun post(url: String): ResponseBody? {
val request = Request.Builder()
.url(url)
.build()
val response = httpClient.newCall(request).execute()
return response.body()
}
}
private class FetchAsyncTask : AsyncTask<Void, Void, String>() {
override fun doInBackground(vararg params: Void): String? {
try {
val result = ApiClass()
.post(url)
return result?.string()
} catch (ioe: Exception) {
ioe.printStackTrace()
}
return null
}
override fun onPostExecute(result: String?) {
Log.i(TAG, "Fetched contents of URL: $result")
}
}
除三星s4(Android版本-5.0.1)外,所有智能手机均可完美兼容。使用它我得到一个错误:
06-29 17:53:18.066 20818-20848/com.example.yashchenkovv.testingurlsamsung W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
at okhttp3.RealCall.execute(RealCall.java:77)
at com.example.yashchenkovv.testingurlsamsung.ApiClass.post(MainActivity.kt:68)
at com.example.yashchenkovv.testingurlsamsung.FetchAsyncTask.doInBackground(MainActivity.kt:34)
at com.example.yashchenkovv.testingurlsamsung.FetchAsyncTask.doInBackground(MainActivity.kt:29)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
有什么想法为什么不起作用?