- Forum-Beiträge: 26
20.10.2017, 08:15:15 via Website
20.10.2017 08:15:15 via Website
Hallo,
aufgrund der letztlich gefundenen Sicherheitslücke im WLAN möchte ich meine Kommunikation von HTTP auf HTTPS mit Zertifikaten umstellen.
Den Server habe ich erfolgreich umgestellt und kann nun nach der installation der Zertifikate alles über https erreichen.
Nun muss ich meine APP auch umstellen und da habe ich so meine Probleme.
Ich habe folgendes zum testen umgesetzt in HTTP:
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("htp://xxxxxx.eu/" +
"mobile.php?get=1", "Get");
}
});
PostBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MakeNetworkCall().execute("htp://xxxxxx.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);
}
}
}
Wie kann ich dieses nun so ändern das es mit HTTPS und Zertifikaten läuft ?
Danke für die unterstützung.
Gruß Ralf
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.