AnsweredAssumed Answered

call my web script

Question asked by dmralfing on Mar 12, 2014
Latest reply on Apr 21, 2014 by kaynezhang
I have implemented a web script that works OK. It works fine writting this in the browser:
    http://localhost:8080/alfresco/service/demo/readonly/prueba
But I want to call it from a java program. The program is:

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

   public static void test (String hostname, String port, String user, String password) throws Exception
   {
      try
      {
         URL_BASE="http://'+hostname+':'+port;
         DefaultHttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(URL_BASE+"/share/page/dologin");
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
         nameValuePairs.add(new BasicNameValuePair("username", user));
         nameValuePairs.add(new BasicNameValuePair("password", password));
         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         client.execute(post);
         setCookieStore(client.getCookieStore());
         executeGET (client.getCookieStore(),"http://localhost:8080/alfresco/service/demo/readonly/prueba");
      }
      catch (Exception ex)
      {
         throw new Exception ("No se pudo realizar la conexión al servidor \""+hostname+ "\" por el puerto "+port+" con el usuario y password indicados.");
      }
      
   }//connect
private static String executeGET (CookieStore cookieStore, String url) throws Exception 
   {
      
      System.out.println(url);
      if (cookieStore==null)
           throw new Exception("ERR. Cookie Store NULO");
      String result=null;
        InputStream instream=null;
        try
        {
            DefaultHttpClient client = new DefaultHttpClient();
            client.setCookieStore(cookieStore);
            HttpGet  httpget = new HttpGet(url);
            HttpResponse  response = client.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity==null) throw new Exception("ERROR. El objeto entity es nulo");
                instream = entity.getContent();
                result = IOUtils.toString(instream);
                System.out.println(result);
              
         }//try
         catch (ClientProtocolException ex){ex.printStackTrace();}
         catch (IOException ex){ex.printStackTrace();}
         catch (Exception ex){ex.printStackTrace();}
         finally{ if (instream!=null) instream.close();}
         return result;
   }//execute

Somebody knows what do I´m doing wrong?

Outcomes