您好我有一個非常惱人的問題與Android中的asynchttpclient。當我嘗試向服務器發送一個請求,我得到這個錯誤:android asynchttpclient javax.net.ssl.SSLHandshakeException:握手失敗
onFailure:: javax.net.ssl.SSLHandshakeException: Handshake failed
我試過郵遞員,瀏覽器和它的作品沒有任何問題,我也得到迴應,但在應用程序不起作用。
服務器有有效的ssl證書,我什至試圖禁用ssl驗證檢查,但仍然得到這個愚蠢的錯誤。
myapikey是我不允許分享你的代碼。
MainActivity.java:
if (getSharedPreferences("guest_token", MODE_PRIVATE).getString("gtkn",null) == null){
AsyncHttpClient client = new AsyncHttpClient(true,80,443);
client.addHeader("Authorization", "myapikey");
client.setSSLSocketFactory(
new SSLSocketFactory(getSslContext(),
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER));
client.get("https://dev.sarshomar.com/api/v1/token/guest", new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
try {
JSONObject m_obj = new JSONObject(responseString);
String guest_token = m_obj.getJSONObject("msg").getJSONObject("callback").getString("token");
SharedPreferences pref = getSharedPreferences("guest_token", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("gtkn", guest_token);
editor.apply();
Log.d("onSuccess_token: ", guest_token);
Toast.makeText(getApplicationContext(),guest_token,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String errorresponse, Throwable throwable) {
throwable.printStackTrace();
Log.d("onFailure: ",throwable.toString()+errorresponse+statusCode+throwable.getLocalizedMessage());
}
@Override
public void onStart() {
// called before request is started
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
}
}
public SSLContext getSslContext() {
TrustManager[] byPassTrustManagers = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
public void checkClientTrusted(X509Certificate[] chain, String authType) {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) {
}
} };
SSLContext sslContext=null;
try {
sslContext = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sslContext.init(null, byPassTrustManagers, new SecureRandom());
} catch (KeyManagementException e) {
e.printStackTrace();
}
return sslContext;
}
我得到的錯誤,我是否嘗試
AsyncHttpClient client = new AsyncHttpClient(true,80,443)
或
AsyncHttpClient client = new AsyncHttpClient();
那麼我該怎麼做?使用另一個httpclient? –
@ mathias.h:如果您需要訪問未經修改的網站,您可能需要使用另一個支持SNI的HTTP客戶端。如果您可以更改網站的配置,則可以將其更改爲不需要SNI。 –