
So the software I am developing in my leisure time is about to complete. Past weekend I worked on it's security aspects and implemented many things like encryption and encapsulation of byte code, Encryption of POST data, Generation of unique ID for each system using my software and the licensing part. I will try to explain each part in my coming posts.
Earlier I demonstrated How to display Splash Screen in Java. Today I will show how I am sending simple data from my Java code to my PHP page on Web server.
package www.isumitjha.com;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
class Test {
public static void main(String args[]) {
try {
// opening a connection to the site
URL url = new URL("http://api.isumitjha.com/example.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// Setting the DoOutput flag true for using output
con.setDoOutput(true);
con.setRequestMethod("POST");
// Encoding the POST data
String data = URLEncoder.encode("key1", "UTF-8") + "=" + URLEncoder.encode("Hello", "UTF-8") + "&" + URLEncoder.encode("key2", "UTF-8") + "=" + URLEncoder.encode("World", "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(
con.getOutputStream());
// Sending parameter to site
osw.write(data);
osw.flush();
con.getInputStream();
System.out.println("RESPONSE CODE = " + con.getResponseCode());
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line = null;
while ((line = in .readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now let's see the simple PHP code that prints all value received via POST request.
<?php
foreach ($_POST as $key => $data) {
echo $data." ";
}
?>
So this is how I am sending data to my server. In my next post I will show you how to encrypt POST data using Advanced Encryption Standard and receive response from server in JSON format.
I hope you enjoyed reading :)
