Sending Simple Data From Java to PHP via a POST Request


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 :)

18 comments:

  1. PHP is widely used programming language used for developing dynamic websites and web applications. You can get clear insights on PHP programming language at training
    Regards,
    PHP Training in Chennai|PHP Training Chennai

    ReplyDelete
  2. Day by day I am getting new things and learn new concept through your blogs, I am feeling so confidants, and thanks for your informative blog keep your post as updated one...
    Regards,
    web designing course in chennai|website designing training

    ReplyDelete
  3. The strategy you have posted on this technology hepled me to get into the next level and had lot of informations in it.
    salesforce training in chennai | salesforce training institute in chennai

    ReplyDelete
  4. The strategy you have posted on this technology hepled me to get into the next level and had lot of informations in it. Python is one of the basic level programming and is very important one.
    Python Training in Chennai | Python Course in Chennai

    ReplyDelete
  5. Excellent Post! Java is such a beautiful language to learn, if you have skills in java, you can easily switch your career in hadoop technology.
    Regards,
    Hadoop Training in Chennai|Best hadoop training institute in chennai|Big Data Training

    ReplyDelete
  6. I agree with your thoughts!!! As the demand of java programming application keeps on increasing, there is massive demand for java professionals in software development industries. Thus, taking training will assist students to be skilled java developers in leading MNCs.

    Salesforce Training in Chennai|cloud computing training in chennai|SEO Training in Chennai

    ReplyDelete
  7. Opportunities in PEGA are increasing day by day. This is a future of technology and it has immense scope. Start your career as PEGA developer.
    PEGA Training center in Chennai | PEGA Training Chennai | PEGA Training institutes in Chennai

    ReplyDelete
  8. Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
    Best hadoop training institute in chennai|Hadoop Training Chennai|Hadoop Training in Chennai

    ReplyDelete
  9. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated.
    Regards,
    cloud computing training in chennai|cloud computing training

    ReplyDelete
  10. Nice and good article.. it is very useful for me to learn and understand easily.. thanks for sharing your valuable information and time.. please keep updating.more php jobs in hyderabad.

    ReplyDelete
  11. Great article. Glad to find your blog. Thanks for sharing.
    sap training institute

    ReplyDelete
  12. Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
    Android application developers Chennai | Android application developers India

    ReplyDelete