【发布时间】:2015-12-02 05:38:18
【问题描述】:
对不起我的英语。我尝试使用库 OKhttp,并使用 https 进行发布请求。现在我有错误,当我尝试发布我的示例时,这是错误:
java.net.UnknownServiceException: Unable to find acceptable protocols. isFallback=false, modes=[ConnectionSpec(cipherSuites=[TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA], tlsVersions=[TLS_1_2], supportsTlsExtensions=true)], supported protocols=[SSLv3, TLSv1]
我尝试修复它,但我不能这样做。我不知道我有什么错误
下面是我的代码:
public class PostOKhttp extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String...ulr) {
Response response = null;
OkHttpClient client = new OkHttpClient();
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA)
.build();
client.setConnectionSpecs(Collections.singletonList(spec));
RequestBody postForm = new FormEncodingBuilder()
.add("name", "name")
.build();
Request request = new Request.Builder()
.url(ulr[0])
.addHeader("id", "--")
.addHeader("key", "--")
.post(postForm)
.build();
try {
response = client.newCall(request).execute();
Log.e("post", response.body().string());
} catch (Exception e) {
Log.e("error", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
}
UDP:
使用 CertificatePinner
我添加此代码
String link = "example.net";
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(link, "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=")
.add(link, "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=")
.add(link, "sha1/blhOM3W9V/bVQhsWAcLYwPU6n24=")
.add(link, "sha1/T5x9IXmcrQ7YuQxXnxoCmeeQ84c=")
.build();
client.setCertificatePinner(certificatePinner);
现在我有这个错误:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
【问题讨论】:
-
那么您的后端是否支持 TLS1.2 以及您列出的至少一种密码套件?
-
@laalto 如果我使用 TLS_1_0 或 TLS_1_1 我有同样的错误
-
输出的 `supportedprotocols=[SSLv3, TLSv1]` 部分表明您的后端仅支持古老的 TLS1.0 和过时的 SSL3,并且没有您声明的密码套件。
-
如果您使用自签名证书,请使用 CertificatePinner 固定它们。但请考虑将您的服务器升级到现代 TLS,而不是恢复到安全性较低的旧协议和密码。
-
@laalto 感谢您的回答。我更新了我的问题,现在我添加了
CertificatePinner,我有错误SSLHandshakeException