martes, 21 de enero de 2014

JAVA STANDALONE COMUNICATION WITH REST PHP SERVER

 public  void restRequest(String urlREST){
    try {
        URLConnection urlConnection;
        //DataOutputStream outStream; //GET method doesn't need an outSream
        DataInputStream inStream;
         
        // Build request body
        //GET Without Body
       
        // Create connection
       // url = new URL("http", "localhost", 8888, "/televisa-dev-api/v3/subscription/renewals");
        URL url = new URL(urlREST);

        //performSubscriptionRenewals
        urlConnection = url.openConnection();
       
        ((HttpURLConnection)urlConnection).setRequestMethod("GET");

        urlConnection.setUseCaches(false);
        urlConnection.setAllowUserInteraction(false);
        urlConnection.setRequestProperty("Content-Type","text/html");

        urlConnection.setRequestProperty("Connection",     "Keep-Alive");
       // urlConnection.setRequestProperty("Content-Length", Integer.toString(Integer.MAX_VALUE));
       // urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36");
        urlConnection.setRequestProperty("Accept","*/*");
//        urlConnection.setRequestProperty("Cookie","SQLiteManager_currentLangue=10");
//        urlConnection.setRequestProperty("Host","localhost:8888");

     
        inStream = new DataInputStream(urlConnection.getInputStream());

 

        // Get Response
        // - For debugging purposes only!
        String buffer;
       
       
        while((buffer = inStream.readLine()) != null) {
            System.out.println(buffer);
        }
        // Close I/O streams
        inStream.close();
    }
    catch(Exception ex) {
        ex.printStackTrace();
        System.out.println("Exception cought:\n"+ ex.toString());
    }
}