我正在尝试从Android平板电脑向api发布一些数据,但我不断收到SSLProtocolException并且我无法解决问题。
这是我不断得到的错误:
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7bccbdc8: Failure in SSL library, usually a protocol error
error:14077458:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 unrecognized name (external/openssl/ssl/s23_clnt.c:741 0x73d67718:0x00000000)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:468)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl$SSLInputStream.<init>(OpenSSLSocketImpl.java:717)
W/System.err﹕ at com.android.org.conscrypt.OpenSSLSocketImpl.getInputStream(OpenSSLSocketImpl.java:688)
W/System.err﹕ at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:70)
W/System.err﹕ at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
W/System.err﹕ at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:221)
W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:167)
W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:125)
W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1227)
W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err﹕ at com.example.casper.bilfinger.sendJSONdata.doInBackground(sendJSONdata.java:130)
W/System.err﹕ at com.example.casper.bilfinger.sendJSONdata.doInBackground(sendJSONdata.java:54)
W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err﹕ W/System.err﹕at java.lang.Thread.run(Thread.java:841)
W/System.err﹕ Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7bccbdc8: Failure in SSL library, usually a protocol error
W/System.err﹕ error:14077458:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 unrecognized name (external/openssl/ssl/s23_clnt.c:741 0x73d67718:0x00000000)
W/System.err﹕ W/System.err﹕at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
W/System.err﹕ W/System.err﹕at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:425)
W/System.err﹕ ... 23 more
这是我正在使用的代码。
public class sendJSONdata extends AsyncTask<String, String, String>{
String httpAuthUser=*username*;
String httpAuthPassword=*password*;
String Bedrijf;
String Naam;
String Functie;
String Vestiging;
String Telefoonnummer;
String Email;
String Opmerkingen;
String GesprokenMet;
Context context;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public sendJSONdata(String[] values,Context context)
{
Bedrijf= values[0];
Naam= values[1];
Functie= values[2];
Vestiging= values[3];
Telefoonnummer= values[4];
Email= values[5];
Opmerkingen= values[6];
GesprokenMet= values[7];
this.context=context;
//System.setProperty("jsse.enableSNIExtension", "false");
}
protected String doInBackground(String... strings) {
///Build JSON object and fill with your data
JSONObject subpostMessage = new JSONObject();
try {
subpostMessage.put("emailAdres", Email);
subpostMessage.put("Naam", Naam);
subpostMessage.put("Bedrijfsnaam", Bedrijf);
subpostMessage.put("Telnummer", Telefoonnummer);
subpostMessage.put("Functie", Functie);
subpostMessage.put("Vestiging", Vestiging);
subpostMessage.put("Opmerkingen", Opmerkingen);
subpostMessage.put("GesprokenMet", GesprokenMet);
} catch (JSONException e) {
e.printStackTrace();
}
//Connect to HttpClient with basic Aut
HttpParams httpParameters = new BasicHttpParams();
HttpClient httpclient = new MyHttpClient(httpParameters,context);//new DefaultHttpClient(httpParameters);//
HttpPost httppost = new HttpPost(*Https to php file");
String base64EncodedCredentials = "Basic " + Base64.encodeToString((httpAuthUser + ":" + httpAuthPassword).getBytes(), Base64.NO_WRAP);
httppost.setHeader("Authorization", base64EncodedCredentials);
String responseStr = null;
try {
// Add your data to te connection
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("postData", subpostMessage.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("httpPost: ",httppost.getMethod());
Log.d("ValuePairs- name: ",nameValuePairs.get(0).getName()+ " val: "+ nameValuePairs.get(0).getValue());
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
//Recover result response
responseStr = EntityUtils.toString(response.getEntity());
Log.d("JSON ATTEMPT","Response: "+responseStr);
} catch (ClientProtocolException e) {
Log.d("JSON ATTEMPT","Response:CPerror "+e.toString());
} catch (IOException e) {
Log.d("JSON ATTEMPT","Response:IOerror "+e.toString());
}
return responseStr;
}
protected void onPostExecute(String result) {
if (result == null) {
//Request Failed
return;
}
//System.out.println("RECEIVED RESPONSE: " + result);
try {
JSONObject jObj = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected void onProgressUpdate(String... values) {
}
}
和它所称的地方。
void postJSONdata()
{
String[] values = new String[8];
values[0]=bnaam.getText().toString();
values[1]=naam.getText().toString();
values[2]=functie.getText().toString();
values[3]=vestiging.getText().toString();
values[4]=tele.getText().toString();
values[5]=mail.getText().toString();
values[6]=opmerking.getText().toString();
values[7]=gesprokenmet.getText().toString();
sendJSONdata task = new sendJSONdata(values,getApplicationContext());
task.execute();
}
我尝试的任何jdk都会抛出此错误(6,7,8), 我发现的大部分帖子都表明它与7中引入的SNI有关,但是6我仍然有同样的错误。
答案 0 :(得分:0)
此问题是由服务器的配置引起的,并且客户端没有或无效支持SNI。如果未使用SNI或SNI使用错误的名称,服务器将返回unrecognized_name警报。因此,您需要确保使用支持SNI的Java版本,启用它并使用正确的名称来访问服务器。