Hallo,
ich versuche gerade eine Kommunikation über https mit Zertifikaten hinzubekommen.
Code
public class MainActivity extends AppCompatActivity {
private static String LOG_TAG = "MainActivity";
TextView TxtResult;
Button PostBtn, GetBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetBtn = (Button) findViewById(R.id.get_btn);
PostBtn = (Button) findViewById(R.id.post_btn);
GetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MakeNetworkCall().execute("https://xxxxxxxx.eu/" +
"mobile.php?get=1", "Get");
}
});
PostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MakeNetworkCall().execute("https://xxxxxxx.eu/" +
"tutorial/http.php?post=1", "Post");
}
});
}
InputStream ByGetMethod(String ServerURL) {
InputStream DataInputStream = null;
try {
URL url = new URL(ServerURL);
HttpURLConnection cc = (HttpURLConnection)
url.openConnection();
//set timeout for reading InputStream
cc.setReadTimeout(5000);
// set timeout for connection
cc.setConnectTimeout(5000);
//set HTTP method to GET
cc.setRequestMethod("GET");
//set it to true as we are connecting for input
cc.setDoInput(true);
//reading HTTP response code
int response = cc.getResponseCode();
//if response code is 200 / OK then read Inputstream
if (response == HttpURLConnection.HTTP_OK) {
DataInputStream = cc.getInputStream();
}
} catch (Exception e) {
Log.e(LOG_TAG, "Error in GetData", e);
}
return DataInputStream;
}
InputStream ByPostMethod(String ServerURL) {
InputStream DataInputStream = null;
try {
//Post parameters
String PostParam = "first_name=android&last_name=pala";
//Preparing
URL url = new URL(ServerURL);
HttpURLConnection cc = (HttpURLConnection)
url.openConnection();
//set timeout for reading InputStream
cc.setReadTimeout(5000);
// set timeout for connection
cc.setConnectTimeout(5000);
//set HTTP method to POST
cc.setRequestMethod("POST");
//set it to true as we are connecting for input
cc.setDoInput(true);
//opens the communication link
cc.connect();
//Writing data (bytes) to the data output stream
DataOutputStream dos = new DataOutputStream(cc.getOutputStream());
dos.writeBytes(PostParam);
//flushes data output stream.
dos.flush();
dos.close();
//Getting HTTP response code
int response = cc.getResponseCode();
//if response code is 200 / OK then read Inputstream
//HttpURLConnection.HTTP_OK is equal to 200
if(response == HttpURLConnection.HTTP_OK) {
DataInputStream = cc.getInputStream();
}
} catch (Exception e) {
Log.e(LOG_TAG, "Error in GetData", e);
}
return DataInputStream;
}
String ConvertStreamToString(InputStream stream) {
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader reader = new BufferedReader(isr);
StringBuilder response = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
response.append(line);
}
} catch (IOException e) {
Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
} catch (Exception e) {
Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
} finally {
try {
stream.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
} catch (Exception e) {
Log.e(LOG_TAG, "Error in ConvertStreamToString", e);
}
}
return response.toString();
}
public void DisplayMessage(String a) {
TxtResult = (TextView) findViewById(R.id.response);
TxtResult.setText(a);
}
private class MakeNetworkCall extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
DisplayMessage("Please Wait ...");
}
@Override
protected String doInBackground(String... arg) {
InputStream is = null;
String URL = arg[0];
Log.d(LOG_TAG, "URL: " + URL);
String res = "";
if (arg[1].equals("Post")) {
is = ByPostMethod(URL);
} else {
is = ByGetMethod(URL);
}
if (is != null) {
res = ConvertStreamToString(is);
} else {
res = "Something went wrong";
}
return res;
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
DisplayMessage(result);
Log.d(LOG_TAG, "Result: " + result);
}
}
}
Alles anzeigen
Leider bekomme ich folgende Fehlermeldung im CatLog:
Code
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Suppressed: javax.net.ssl.SSLHandshakeException: Handshake failed
... 22 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c543e:SSL routines:ssl3_read_bytes:TLSV1_ALERT_INAPPROPRIATE_FALLBACK (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c543e:SSL routines:ssl3_read_bytes:TLSV1_ALERT_INAPPROPRIATE_FALLBACK (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc680:0x00000001)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:353)
... 21 more
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x9f33cc00: Failure in SSL library, usually a protocol error
error:100c5410:SSL routines:ssl3_read_bytes:SSLV3_ALERT_HANDSHAKE_FAILURE (external/boringssl/src/ssl/s3_pkt.c:972 0xa95bc800:0x00000001)
error:100c009f:SSL routines:ssl3_get_server_hello:HANDSHAKE_FAILURE_ON_CLIENT_HELLO (external/boringssl/src/ssl/s3_clnt.c:771 0xa93747f7:0x00000000)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Nati
10-20 22:44:44.237 4511-4511/? E/Zygote: v2
Alles anzeigen
Wo mache ich was falsch oder wie muß ich anders machen damit es funktioniert ?
Danke
Ralf Kruppa